{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Table of Contents\n",
"1. [Standard Plots](#Standard-Plots)\n",
" 1. [Line plot 2d](#Line-plot-2d)\n",
" 1. [Line plot 2d (2)](#Line-plot-2d-(2))\n",
" 1. [Datetime plot](#Datetime-plot)\n",
" 1. [Function plot](#Function-plot)\n",
" 1. [simple function](#simple-function)\n",
" 1. [parametric function plot](#parametric-function-plot)\n",
" 1. [parametric function plot 3d](#parametric-function-plot-3d)\n",
" 1. [log plot](#log-plot)\n",
" 1. [semilog y](#semilog-y)\n",
" 1. [semilog x](#semilog-x)\n",
" 1. [log log](#log-log)\n",
" 1. [Bar plot](#Bar-plot)\n",
" 1. [Vertical Bar Plot](#Vertical-Bar-Plot)\n",
" 1. [stacked bar plot](#stacked-bar-plot)\n",
" 1. [Horizontal Bar Plot](#Horizontal-Bar-Plot)\n",
" 1. [Histogram Plot](#Histogram-Plot)\n",
" 1. [Histogram Plot](#Histogram-Plot)\n",
" 1. [Categorical Histogram Plot](#Categorical-Histogram-Plot)\n",
" 1. [Hisogram plot 2d](#Hisogram-plot-2d)\n",
" 1. [Box plot](#Box-plot)\n",
" 1. [Box plot (1)](#Box-plot-(1))\n",
" 1. [Box plot (2)](#Box-plot-(2))\n",
" 1. [Heatmap](#Heatmap)\n",
" 1. [Pie](#Pie)\n",
" 1. [Contour plot](#Contour-plot)\n",
" 1. [Contour plot](#Contour-plot)\n",
" 1. [Function Contour plot](#Function-Contour-plot)\n",
" 1. [polar plots](#polar-plots)\n",
" 1. [Rose plot](#Rose-plot)\n",
" 1. [Scatter plot](#Scatter-plot)\n",
" 1. [Scatter plot 2d](#Scatter-plot-2d)\n",
" 1. [scatter plot 3d](#scatter-plot-3d)\n",
" 1. [Stem plot](#Stem-plot)\n",
" 1. [Stairstep plot](#Stairstep-plot)\n",
" 1. [Error bar](#Error-bar)\n",
" 1. [Two y axis plot](#Two-y-axis-plot)\n",
" 1. [Subplot](#Subplot)\n",
" 1. [subplot (1)](#subplot-(1))\n",
" 1. [subplot (2)](#subplot-(2))\n",
" 1. [subplot (3)](#subplot-(3))\n",
" 1. [Quiver plots](#Quiver-plots)\n",
" 1. [Surface](#Surface)\n",
" 1. [Surface (1)](#Surface-(1))\n",
" 1. [Surface (2)](#Surface-(2))\n",
" 1. [Surface (3)](#Surface-(3))\n",
"1. [Customizing plots](#Customizing-plots)\n",
" 1. [Standard line colors](#Standard-line-colors)\n",
" 1. [Line style](#Line-style)\n",
" 1. [Adding LaTeX to plots (1)](#Adding-LaTeX-to-plots-(1))\n",
" 1. [Adding LaTeX to plots (2)](#Adding-LaTeX-to-plots-(2))\n",
" 1. [Axis](#Axis)\n",
" 1. [axis](#axis)\n",
" 1. [No axis](#No-axis)\n",
" 1. [ticks](#ticks)\n",
" 1. [Formatter](#Formatter)\n",
" 1. [axis range](#axis-range)\n",
" 1. [Aspect ratio](#Aspect-ratio)\n",
" 1. [axis flip](#axis-flip)\n",
" 1. [Font](#Font)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Julia Version 1.7.0\n",
"Commit 3bf9d17731 (2021-11-30 12:12 UTC)\n",
"Platform Info:\n",
" OS: Linux (x86_64-pc-linux-gnu)\n",
" CPU: AMD Ryzen 5 PRO 3400GE w/ Radeon Vega Graphics\n",
" WORD_SIZE: 64\n",
" LIBM: libopenlibm\n",
" LLVM: libLLVM-12.0.1 (ORCJIT, znver1)\n",
"Environment:\n",
" JULIA_VERSION = 1.7.0\n"
]
}
],
"source": [
"using InteractiveUtils\n",
"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 `~/PlotsGallery`\n"
]
}
],
"source": [
"using Pkg\n",
"Pkg.activate(\".\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Pkg.update()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"┌ Warning: The active manifest file is an older format with no julia version entry. Dependencies may have been resolved with a different julia version.\n",
"└ @ nothing /home/jovyan/PlotsGallery/Manifest.toml:0\n"
]
}
],
"source": [
"Pkg.instantiate()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"Pkg.precompile()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[32m\u001b[1m Status\u001b[22m\u001b[39m `~/PlotsGallery/Project.toml`\n",
" \u001b[90m [28b8d3ca] \u001b[39mGR v0.62.1\n",
" \u001b[90m [7073ff75] \u001b[39mIJulia v1.23.2\n",
" \u001b[90m [682c06a0] \u001b[39mJSON v0.21.2\n",
" \u001b[90m [b964fa9f] \u001b[39mLaTeXStrings v1.3.0\n",
" \u001b[90m [98b081ad] \u001b[39mLiterate v2.9.4\n",
" \u001b[90m [442fdcdd] \u001b[39mMeasures v0.3.1\n",
" \u001b[90m [995b91a9] \u001b[39mPlotUtils v1.0.15\n",
" \u001b[90m [91a5bcdd] \u001b[39mPlots v1.25.1\n",
" \u001b[90m [438e738f] \u001b[39mPyCall v1.92.5\n",
" \u001b[90m [d330b81b] \u001b[39mPyPlot v2.10.0\n",
" \u001b[90m [276daf66] \u001b[39mSpecialFunctions v1.8.1\n",
" \u001b[90m [2913bbd2] \u001b[39mStatsBase v0.33.13\n",
" \u001b[90m [f3b207a7] \u001b[39mStatsPlots v0.14.29\n",
" \u001b[90m [fa267f1f] \u001b[39mTOML\n"
]
}
],
"source": [
"Pkg.status()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Plots.GRBackend()"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ENV[\"GKS_ENCODING\"]=\"utf-8\" # for scientific formatter\n",
"using Plots;\n",
"# pyplot()\n",
"gr()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Standard Plots"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Line plot 2d"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = 0:0.1:2pi\n",
"y1 = cos.(x)\n",
"y2 = sin.(x)\n",
"\n",
"plot(x, y1, c=\"blue\", linewidth=3)\n",
"plot!(x, y2, c=\"red\", line=:dash)\n",
"title!(\"Trigonometric functions\")\n",
"xlabel!(\"angle\")\n",
"ylabel!(\"sin(x) and cos(x)\")\n",
"\n",
"# axis limit\n",
"plot!(xlims=(0,2pi), ylims=(-2, 2))"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# same plot\n",
"x = 0:0.1:2pi\n",
"y1 = cos.(x)\n",
"y2 = sin.(x)\n",
"\n",
"plot(x, y1,\n",
" c=\"blue\",\n",
" linewidth=3,\n",
" title=\"Trigonometric functions\",\n",
" xlabel=\"angle\",\n",
" ylabel=\"sin(x) and cos(x)\")\n",
"plot!(x, y2, c=\"red\", line=:dash)\n",
"\n",
"plot!(xlims=(0,2pi), ylims=(-2, 2))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Line plot 2d (2)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"using Random\n",
"Random.seed!(2018)\n",
"\n",
"time = 30\n",
"warlker1 = cumsum(randn(time))\n",
"warlker2 = cumsum(randn(time))\n",
"warlker3 = cumsum(randn(time))\n",
"warlker4 = cumsum(randn(time))\n",
"warlker5 = cumsum(randn(time))\n",
"\n",
"plot(1:time, [warlker1 warlker2 warlker3 warlker4 warlker5],\n",
" xlabel=\"time\",\n",
" ylabel=\"position\",\n",
" label=[\"walker1\" \"walker2\" \"walker3\" \"walker4\" \"walker5\"],\n",
" leg=:bottomleft)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Datetime plot\n",
"https://github.com/JuliaPlots/Plots.jl/issues/356"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"using Measures, Random, Dates\n",
"\n",
"Random.seed!(2018)\n",
"days = 31\n",
"position = cumsum(randn(days))\n",
"x = Date(2018,1,1):Day(1):Date(2018,1,31)\n",
"ticks = [x[i] for i in 1:5:length(x)]\n",
"\n",
"plot(x, position,\n",
" xlabel=\"Date\",\n",
" ylabel=\"Position\",\n",
" title=\"Track of random walker\",\n",
" xticks=ticks,\n",
" xrotation=45,\n",
" bottom_margin=15mm,\n",
" left_margin=5mm)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Function plot\n",
"### simple function"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f(x) = 5exp(-x^2)\n",
"g(x) = x^2\n",
"\n",
"plot([f, g], -3, 3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### parametric function plot\n",
"http://docs.juliaplots.org/latest/examples/pyplot/"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xₜ(t) = sin(t)\n",
"yₜ(t) = sin(2t)\n",
"\n",
"plot(xₜ, yₜ, 0, 2π, leg=false, fill=(0,:orange))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### parametric function plot 3d\n",
"https://jp.mathworks.com/matlabcentral/fileexchange/35263-matlab-plot-gallery-function-plot-3d?focused=7851891&tab=example"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t = range(0, stop=10, length=1000)\n",
"x = cos.(t)\n",
"y = sin.(t)\n",
"z = sin.(5t)\n",
"\n",
"plot(x, y, z)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## log plot\n",
"### semilog y"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"plot(exp, -5, 5, yscale=:log10, title=\"semilogy\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### semilog x"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"plot(log, 0+0.00001, 5, xscale=:log10, title=\"semilogx\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### log log"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f(x) = x^-1.7\n",
"plot(f, 1e-3, 3, scale=:log10, title=\"log-log\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Bar plot\n",
"### Vertical Bar Plot"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"https://jp.mathworks.com/matlabcentral/fileexchange/35271-matlab-plot-gallery-vertical-bar-plot?focused=6793087&tab=example"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"using StatsPlots\n",
"\n",
"measles = [38556, 24472, 14556, 18060, 19549, 8122, 28541, 7880, 3283, 4135, 7953, 1884]\n",
"mumps = [20178, 23536, 34561, 37395, 36072, 32237, 18597, 9408, 6005, 6268, 8963, 13882]\n",
"chickenPox = [37140, 32169, 37533, 39103, 33244, 23269, 16737, 5411, 3435, 6052, 12825, 23332]\n",
"\n",
"ticklabel = string.(collect('A':'L'))\n",
"groupedbar([measles mumps chickenPox], bar_position = :dodge, bar_width=0.7, xticks=(1:12, ticklabel))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### stacked bar plot"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"using StatsPlots\n",
"\n",
"measles = [38556, 24472, 14556, 18060, 19549, 8122, 28541, 7880, 3283, 4135, 7953, 1884]\n",
"mumps = [20178, 23536, 34561, 37395, 36072, 32237, 18597, 9408, 6005, 6268, 8963, 13882]\n",
"chickenPox = [37140, 32169, 37533, 39103, 33244, 23269, 16737, 5411, 3435, 6052, 12825, 23332]\n",
"\n",
"# In PyPlot backend, if we use chars like 'A':'L', ticks are displayed with \"PyWrap\".\n",
"ticklabel = string.(collect('A':'L'))\n",
"groupedbar([measles mumps chickenPox],\n",
" bar_position = :stack,\n",
" bar_width=0.7,\n",
" xticks=(1:12, ticklabel),\n",
" label=[\"measles\" \"mumps\" \"chickenPox\"])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Horizontal Bar Plot"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ticklabel = string.(collect('a':'l'))\n",
"bar(1:12, orientation=:h, yticks=(1:12, ticklabel), yflip=true)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Histogram Plot\n",
"### Histogram Plot"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"using Random\n",
"Random.seed!(2018)\n",
"\n",
"x = randn(1000)\n",
"y = randn(1000)\n",
"z = randn(1000)\n",
"\n",
"histogram(x, bins=20, alpha=0.4, label=\"A\")\n",
"histogram!(y, bins=20, alpha=0.6, label=\"B\")\n",
"histogram!(z, bins=20, alpha=0.8, label=\"C\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Categorical Histogram Plot"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"https://jp.mathworks.com/matlabcentral/fileexchange/63452-matlab-plot-gallery-categorical-histogram-plot?focused=7852078&tab=example"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"using StatsBase, Random\n",
"\n",
"Random.seed!(2018)\n",
"s = [\"Poor\", \"Fair\", \"Good\", \"Excellent\"]\n",
"data = sample(s, Weights([1,1,2,2]), 100)\n",
"datamap = countmap(data)\n",
"bar((x -> datamap[x]).(s), xticks=(1:4, s))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Hisogram plot 2d"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = randn(50000);\n",
"y = x * 1.2 + randn(50000);\n",
"\n",
"plot(\n",
" x,\n",
" y,\n",
" seriestype = :histogram2d,\n",
" c = :vik,\n",
" nbins = 200,\n",
" show_empty_bins = :true\n",
")\n",
"\n",
"# equivalent\n",
"# histogram2d(\n",
"# x,\n",
"# y,\n",
"# c = :vik,\n",
"# nbins = 200,\n",
"# show_empty_bins = :true\n",
"# )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Box plot\n",
"### Box plot (1)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"median(science) = 5.5\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"┌ Warning: Keyword argument hover not supported with Plots.GRBackend(). Choose from: Set([:top_margin, :group, :inset_subplots, :background_color, :ytickfontsize, :yforeground_color_text, :yguidefontcolor, :tickfontfamily, :show_empty_bins, :seriesalpha, :seriescolor, :ztick_direction, :xgrid, :ygridalpha, :zlims, :xtick_direction, :colorbar, :legend_font_family, :zflip, :ticks, :linealpha, :overwrite_figure, :arrow, :xguidefonthalign, :normalize, :linestyle, :xtickfontvalign, :xflip, :zgrid, :fillcolor, :ygrid, :bar_width, :colorbar_scale, :background_color_inside, :zguidefonthalign, :bins, :zguide, :zforeground_color_text, :legend_font_valign, :yscale, :legend_font_color, :weights, :xgridalpha, :ygridstyle, :clims, :xtickfontcolor, :fill_z, :xguide, :markershape, :background_color_subplot, :ztickfontfamily, :fillalpha, :markerstrokewidth, :tick_direction, :xguidefontvalign, :xguidefontfamily, :gridlinewidth, :foreground_color_subplot, :xgridlinewidth, :yguidefontsize, :foreground_color, :foreground_color_text, :titlefonthalign, :yerror, :x, :xtickfonthalign, :zgridlinewidth, :ytickfontrotation, :discrete_values, :ytick_direction, :grid, :xguidefontrotation, :ribbon, :xguidefontsize, :tickfontrotation, :xforeground_color_axis, :xdiscrete_values, :background_color_outside, :titlefontcolor, :xgridstyle, :line_z, :size, :orientation, :gridstyle, :projection, :markersize, :legend_foreground_color, :camera, :zguidefontrotation, :ydiscrete_values, :xforeground_color_grid, :seriestype, :yflip, :quiver, :zticks, :markerstrokecolor, :ztickfontrotation, :ztickfonthalign, :fillrange, :ztickfontvalign, :xlims, :xforeground_color_border, :markercolor, :xtickfontsize, :ylink, :levels, :color_palette, :connections, :yforeground_color_grid, :lims, :zgridstyle, :foreground_color_border, :zguidefontvalign, :xscale, :marker_z, :markerstrokealpha, :left_margin, :markeralpha, :legend_font_halign, :annotations, :window_title, :tickfontvalign, :foreground_color_axis, :zguidefontcolor, :ygridlinewidth, :zlink, :zscale, :smooth, :yguidefontrotation, :xticks, :guidefontsize, :zguidefontsize, :y, :margin, :ytickfontcolor, :zdiscrete_values, :tickfonthalign, :bottom_margin, :yforeground_color_border, :zguidefontfamily, :framestyle, :yguidefontvalign, :yguidefonthalign, :zerror, :zgridalpha, :ztickfontcolor, :scale, :legend_position, :linecolor, :html_output_format, :legend_title, :zforeground_color_border, :legend_font_pointsize, :title, :tickfontcolor, :subplot_index, :flip, :titlefontrotation, :legend_background_color, :tickfontsize, :titlefontvalign, :z, :yforeground_color_axis, :foreground_color_grid, :xtickfontrotation, :linewidth, :ztickfontsize, :gridalpha, :xerror, :guidefontfamily, :ylims, :contour_labels, :xguidefontcolor, :primary, :xtickfontfamily, :ytickfontvalign, :guidefonthalign, :ytickfontfamily, :aspect_ratio, :xforeground_color_text, :show, :link, :colorbar_title, :guidefontrotation, :subplot, :label, :ytickfonthalign, :guide, :guidefontcolor, :yguide, :titlefontsize, :titlefontfamily, :guidefontvalign, :zforeground_color_axis, :zforeground_color_grid, :layout, :legend_font_rotation, :colorbar_entry, :yguidefontfamily, :polar, :right_margin, :xlink, :series_annotations, :yticks])\n",
"└ @ Plots /home/jovyan/.julia/packages/Plots/Zuo5g/src/args.jl:1607\n"
]
},
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"using StatsPlots, Statistics, Random\n",
"Random.seed!(2018)\n",
"\n",
"n = 30\n",
"science = rand(1:10, n)\n",
"@show median(science)\n",
"boxplot(science, label=permutedims([\"science\"]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Box plot (2)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"median(science) = 5.5\n",
"median(english) = 6.0\n"
]
},
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# This outputs BoundsError. This is a bug of StatPlots.jl.\n",
"# Boxplot and violin broken - https://github.com/JuliaPlots/StatPlots.jl/issues/198\n",
"using StatsPlots, Statistics, Random\n",
"Random.seed!(2018)\n",
"\n",
"n = 30\n",
"science = rand(1:10, n)\n",
"english = rand(1:10, n)\n",
"\n",
"@show median(science)\n",
"@show median(english)\n",
"\n",
"boxplot([science english], label=[\"science\" \"english\"])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Heatmap"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"using Random\n",
"Random.seed!(2018)\n",
"\n",
"a = rand(5,5)\n",
"xlabel = string.(collect('A':'E'))\n",
"ylabel = string.(collect('a':'e'))\n",
"heatmap(a, xticks=(1:5, xlabel), yticks=(1:5, ylabel),\n",
" fill_z=a, aspect_ratio=:equal)\n",
"\n",
"fontsize = 15\n",
"nrow, ncol = size(a)\n",
"ann = [(i,j, text(round(a[i,j], digits=2), fontsize, :white, :center))\n",
" for i in 1:nrow for j in 1:ncol]\n",
"annotate!(ann, linecolor=:white)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Pie"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# ref https://docs.juliaplots.org/latest/generated/pyplot/\n",
"x = [\"Nerds\",\"Hackers\",\"Scientists\"]\n",
"y = [0.4,0.35,0.25]\n",
"pie(x, y, title=\"The Julia Community\",l = 0.5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Contour plot\n",
"### Contour plot"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f(x,y) = x^2 + y^2\n",
"x = repeat(range(0, stop=2, length=50), 1, 50)\n",
"y = repeat(range(0, stop=2, length=50), 1, 50)'\n",
"\n",
"contour( f.(x, y) )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Function Contour plot"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# ref https://jp.mathworks.com/matlabcentral/fileexchange/35283-matlab-plot-gallery-function-contour-plot?focused=7851904&tab=example\n",
"f(x,y) = sin(3x) * cos(x+y)\n",
"\n",
"x = 0:0.01:5\n",
"y = 0:0.01:5\n",
"contour(x, y, f, xlabel=\"x\", ylabel=\"y\", fill=true)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## polar plots"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# ref https://jp.mathworks.com/matlabcentral/fileexchange/35285-matlab-plot-gallery-function-polar-plot?focused=6793167&tab=example\n",
"r(θ) = 1 + cos(θ) * sin(θ)^2\n",
"plot(r, 0, 2π, proj=:polar, lims=(0,1.5))"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"r(θ) = 1 + cos(θ) * sin(θ)^2\n",
"θ = range(0, stop=2π, length=50)\n",
"plot(θ, r.(θ), proj=:polar, lims=(0,1.5))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Rose plot"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"using Random\n",
"Random.seed!(2018)\n",
"\n",
"n = 24\n",
"R = rand(n+1)\n",
"θ = 0:2pi/n:2pi\n",
"plot(θ, R, proj=:polar, line=:steppre, lims=(0,1))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Scatter plot\n",
"### Scatter plot 2d"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"using Random\n",
"Random.seed!(2018)\n",
"\n",
"n = 50\n",
"x = rand(n)\n",
"y = rand(n)\n",
"ms = rand(50) * 30\n",
"scatter(x, y, markersize=ms)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### scatter plot 3d"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"using Random\n",
"Random.seed!(2018)\n",
"\n",
"n = 50\n",
"x = rand(n)\n",
"y = rand(n)\n",
"z = rand(n)\n",
"ms = rand(50) * 30\n",
"scatter(x, y, z, markersize=ms)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Stem plot"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = sin.(0:0.3:2pi)\n",
"plot(x, line=:stem, marker=:star, markersize=20)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Stairstep plot"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = sin.(0:0.3:2pi)\n",
"plot(x, line=:steppre, markersize=20)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Error bar"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"using Random\n",
"Random.seed!(2018)\n",
"\n",
"f(x) = 2 * x + 1\n",
"x = 0:0.1:2\n",
"n = length(x)\n",
"y = f.(x) + randn(n)\n",
"plot(x, y,\n",
" xerr=0.1 * rand(n),\n",
" yerr=rand(n))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Two y axis plot"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# pyplot()\n",
"using Measures, Random\n",
"\n",
"Random.seed!(2018)\n",
"plot(randn(100), ylabel=\"y1\", leg=:topright)\n",
"plot!(twinx(), randn(100)*10,\n",
" c=:red,\n",
" ylabel=\"y2\",\n",
" leg=:bottomright,\n",
" size=(600, 400))\n",
"plot!(right_bottom=10mm)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Subplot\n",
"### subplot (1)"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# gr()\n",
"a = plot(sin, 0, 2pi, xlabel=\"x1\")\n",
"b = plot(cos, 0, 2pi, xlabel=\"x2\")\n",
"c = histogram(randn(1000), xlabel=\"x3\")\n",
"d = plot(x->exp(-x^2), -3, 3, xlabel=\"x4\")\n",
"\n",
"plot(a, b, c, d)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### subplot (2)"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = plot(sin, 0, 2pi, xlabel=\"x1\")\n",
"b = plot(cos, 0, 2pi, xlabel=\"x2\")\n",
"c = histogram(randn(1000), xlabel=\"x3\")\n",
"d = plot(x->exp(-x^2), -3, 3, xlabel=\"x4\")\n",
"\n",
"plot(a, b, c, d, layout=(1,4), size=(1000, 300))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### subplot (3)"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = plot(sin, 0, 2pi, xlabel=\"x1\")\n",
"b = plot(cos, 0, 2pi, xlabel=\"x2\")\n",
"c = histogram(randn(1000), xlabel=\"x3\")\n",
"d = plot(x->exp(-x^2), -3, 3, xlabel=\"x4\")\n",
"\n",
"plot(a, b, c, d, layout=(4,1), size=(300, 800))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Quiver plots"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"n = 7\n",
"f(x,y) = 1 / hypot(x, y)\n",
"x = repeat(-3:(2*3)/n:3, 1, n) |> vec\n",
"y = repeat(-3:(2*3)/n:3, 1, n)' |> vec\n",
"vx = f.(x,y) .* cos.(atan.(y,x)) |> vec\n",
"vy = f.(x,y) .* sin.(atan.(y,x)) |> vec\n",
"\n",
"quiver(x, y, quiver=(vx, vy), aspect_ratio=:equal)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Surface\n",
"### Surface (1)"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f(x,y) = x^2 + y^2\n",
"x = -10:10\n",
"y = x\n",
"surface(x, y, f)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Surface (2)"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f(x,y) = x^2 + y^2\n",
"x = -10:10\n",
"y = x\n",
"plot(x, y, f, linetype=:surface)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Surface (3)"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"