{ "cells": [ { "cell_type": "raw", "metadata": {}, "source": [ "---\n", "title: \"Notes: Plotting in Julia\"\n", "subtitle: Advanced Statistical Computing\n", "author: Joong-Ho Won\n", "date: today\n", "date-format: \"MMMM YYYY\"\n", "institute: Seoul National University\n", "execute:\n", " echo: true\n", "format:\n", " html:\n", " toc: false\n", " code-fold: false \n", "jupyter: julia \n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plotting in Julia\n", "\n", "* Plotting in Julia is conducted via [Plots.jl](https://github.com/JuliaPlots/Plots.jl) metapackage.\n", " - Defines an unified interface for plotting\n", " - Maps arguments to different plotting [backends](https://docs.juliaplots.org/latest/backends/#backends)\n", " - GR (default), PythonPlot, Plotly/PlotlyJS, and many more \n", " - Mapping of attributes to backends: \n", " - First runs can be slow. GR backend seems fastest and most stable." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Julia Version 1.9.3\n", "Commit bed2cd540a1 (2023-08-24 14:43 UTC)\n", "Build Info:\n", " Official https://julialang.org/ release\n", "Platform Info:\n", " OS: macOS (x86_64-apple-darwin22.4.0)\n", " CPU: 8 × Intel(R) Core(TM) i5-8279U CPU @ 2.40GHz\n", " WORD_SIZE: 64\n", " LIBM: libopenlibm\n", " LLVM: libLLVM-14.0.6 (ORCJIT, skylake)\n", " Threads: 2 on 8 virtual cores\n" ] } ], "source": [ "versioninfo()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\u001b[32m\u001b[1m Activating\u001b[22m\u001b[39m project at `~/Dropbox/class/M1399.000200/2023/M1300_000200-2023fall/lectures/02-juliaintro`\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[32m\u001b[1mStatus\u001b[22m\u001b[39m `~/Dropbox/class/M1399.000200/2023/M1300_000200-2023fall/lectures/02-juliaintro/Project.toml`\n", " \u001b[90m[31c24e10] \u001b[39mDistributions v0.25.100\n", " \u001b[90m[f0f68f2c] \u001b[39mPlotlyJS v0.18.10\n", " \u001b[90m[91a5bcdd] \u001b[39mPlots v1.39.0\n", " \u001b[90m[274fc56d] \u001b[39mPythonPlot v1.0.3\n" ] } ], "source": [ "using Pkg\n", "Pkg.activate(pwd())\n", "Pkg.instantiate()\n", "Pkg.status()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plots.jl\n", "\n", "We demonstrate Plots.jl below:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "using Plots, Random\n", "\n", "Random.seed!(123) # set seed\n", "x = cumsum(randn(50, 2), dims=1);" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "gr() # default backend\n", "Plots.plot(x, title=\"Random walk\", xlab=\"time\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "using PythonPlot # WARNING: this installs a matplotlib on its own; it takes ~300MB\n", "pythonplot() # set the backend to PythonPlot\n", "Plots.plot(x, title=\"Random walk\", xlab=\"time\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "using PlotlyJS\n", "plotlyjs() # set the backend to PlotlyJS\n", "Plots.plot(x, title=\"Random walk\", xlab=\"time\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "gr()\n", "@gif for i in 1:20 # animated GIF\n", " Plots.plot(x -> sin(x) / (.2i), 0, i, xlim=(0, 20), ylim=(-.75, .75))\n", " scatter!(x -> cos(x) * .01 * i, 0, i, m=1)\n", "end;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](tmp.gif)" ] } ], "metadata": { "kernelspec": { "display_name": "Julia 1.9.3", "language": "julia", "name": "julia-1.9" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.9.3" }, "toc": { "colors": { "hover_highlight": "#DAA520", "running_highlight": "#FF0000", "selected_highlight": "#FFD700" }, "moveMenuLeft": true, "nav_menu": { "height": "66.52174377441406px", "width": "251.7391357421875px" }, "navigate_menu": true, "number_sections": true, "sideBar": true, "skip_h1_title": true, "threshold": 4, "toc_cell": true, "toc_section_display": "block", "toc_window_display": true, "widenNotebook": false } }, "nbformat": 4, "nbformat_minor": 4 }