In [1]:
using InteractiveUtils
versioninfo()
Julia Version 1.7.0
Commit 3bf9d17731 (2021-11-30 12:12 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: AMD Ryzen 5 PRO 3400GE w/ Radeon Vega Graphics
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-12.0.1 (ORCJIT, znver1)
Environment:
  JULIA_VERSION = 1.7.0
In [2]:
using Pkg
Pkg.activate(".")
  Activating project at `~/PlotsGallery`
In [3]:
# Pkg.update()
In [4]:
Pkg.instantiate()
┌ Warning: The active manifest file is an older format with no julia version entry. Dependencies may have been resolved with a different julia version.
└ @ nothing /home/jovyan/PlotsGallery/Manifest.toml:0
In [5]:
Pkg.precompile()
In [6]:
Pkg.status()
      Status `~/PlotsGallery/Project.toml`
  [28b8d3ca] GR v0.62.1
  [7073ff75] IJulia v1.23.2
  [682c06a0] JSON v0.21.2
  [b964fa9f] LaTeXStrings v1.3.0
  [98b081ad] Literate v2.9.4
  [442fdcdd] Measures v0.3.1
  [995b91a9] PlotUtils v1.0.15
  [91a5bcdd] Plots v1.25.1
  [438e738f] PyCall v1.92.5
  [d330b81b] PyPlot v2.10.0
  [276daf66] SpecialFunctions v1.8.1
  [2913bbd2] StatsBase v0.33.13
  [f3b207a7] StatsPlots v0.14.29
  [fa267f1f] TOML
In [7]:
ENV["GKS_ENCODING"]="utf-8" # for scientific formatter
using Plots;
# pyplot()
gr()
Out[7]:
Plots.GRBackend()

Standard Plots

Line plot 2d

In [8]:
x = 0:0.1:2pi
y1 = cos.(x)
y2 = sin.(x)

plot(x, y1, c="blue", linewidth=3)
plot!(x, y2, c="red", line=:dash)
title!("Trigonometric functions")
xlabel!("angle")
ylabel!("sin(x) and cos(x)")

# axis limit
plot!(xlims=(0,2pi), ylims=(-2, 2))
Out[8]:
In [9]:
# same plot
x = 0:0.1:2pi
y1 = cos.(x)
y2 = sin.(x)

plot(x, y1,
    c="blue",
    linewidth=3,
    title="Trigonometric functions",
    xlabel="angle",
    ylabel="sin(x) and cos(x)")
plot!(x, y2, c="red", line=:dash)

plot!(xlims=(0,2pi), ylims=(-2, 2))
Out[9]:

Line plot 2d (2)

In [10]:
using Random
Random.seed!(2018)

time = 30
warlker1 = cumsum(randn(time))
warlker2 = cumsum(randn(time))
warlker3 = cumsum(randn(time))
warlker4 = cumsum(randn(time))
warlker5 = cumsum(randn(time))

plot(1:time, [warlker1 warlker2 warlker3 warlker4 warlker5],
    xlabel="time",
    ylabel="position",
    label=["walker1" "walker2" "walker3" "walker4" "walker5"],
    leg=:bottomleft)
Out[10]:
In [11]:
using Measures, Random, Dates

Random.seed!(2018)
days = 31
position = cumsum(randn(days))
x = Date(2018,1,1):Day(1):Date(2018,1,31)
ticks = [x[i] for i in 1:5:length(x)]

plot(x, position,
    xlabel="Date",
    ylabel="Position",
    title="Track of random walker",
    xticks=ticks,
    xrotation=45,
    bottom_margin=15mm,
    left_margin=5mm)
Out[11]:

Function plot

simple function

In [12]:
f(x) = 5exp(-x^2)
g(x) = x^2

plot([f, g], -3, 3)
Out[12]:
In [13]:
xₜ(t) = sin(t)
yₜ(t) = sin(2t)

plot(xₜ, yₜ, 0, 2π, leg=false, fill=(0,:orange))
Out[13]:
In [14]:
t = range(0, stop=10, length=1000)
x = cos.(t)
y = sin.(t)
z = sin.(5t)

plot(x, y, z)
Out[14]:

log plot

semilog y

In [15]:
plot(exp, -5, 5, yscale=:log10, title="semilogy")
Out[15]:

semilog x

In [16]:
plot(log, 0+0.00001, 5, xscale=:log10, title="semilogx")
Out[16]:

log log

In [17]:
f(x) = x^-1.7
plot(f, 1e-3, 3, scale=:log10, title="log-log")
Out[17]:

Bar plot

Vertical Bar Plot

In [18]:
using StatsPlots

measles = [38556, 24472, 14556, 18060, 19549, 8122, 28541, 7880, 3283, 4135, 7953, 1884]
mumps = [20178, 23536, 34561, 37395, 36072, 32237, 18597, 9408, 6005, 6268, 8963, 13882]
chickenPox = [37140, 32169, 37533, 39103, 33244, 23269, 16737, 5411, 3435, 6052, 12825, 23332]

ticklabel = string.(collect('A':'L'))
groupedbar([measles mumps chickenPox], bar_position = :dodge, bar_width=0.7, xticks=(1:12, ticklabel))
Out[18]:

stacked bar plot

In [19]:
using StatsPlots

measles = [38556, 24472, 14556, 18060, 19549, 8122, 28541, 7880, 3283, 4135, 7953, 1884]
mumps = [20178, 23536, 34561, 37395, 36072, 32237, 18597, 9408, 6005, 6268, 8963, 13882]
chickenPox = [37140, 32169, 37533, 39103, 33244, 23269, 16737, 5411, 3435, 6052, 12825, 23332]

# In PyPlot backend, if we use chars like 'A':'L', ticks are displayed with "PyWrap".
ticklabel = string.(collect('A':'L'))
groupedbar([measles mumps chickenPox],
        bar_position = :stack,
        bar_width=0.7,
        xticks=(1:12, ticklabel),
        label=["measles" "mumps" "chickenPox"])
Out[19]:

Horizontal Bar Plot

In [20]:
ticklabel = string.(collect('a':'l'))
bar(1:12, orientation=:h, yticks=(1:12, ticklabel), yflip=true)
Out[20]:

Histogram Plot

Histogram Plot

In [21]:
using Random
Random.seed!(2018)

x = randn(1000)
y = randn(1000)
z = randn(1000)

histogram(x, bins=20, alpha=0.4, label="A")
histogram!(y, bins=20, alpha=0.6, label="B")
histogram!(z, bins=20, alpha=0.8, label="C")
Out[21]:

Categorical Histogram Plot

In [22]:
using StatsBase, Random

Random.seed!(2018)
s = ["Poor", "Fair", "Good", "Excellent"]
data = sample(s, Weights([1,1,2,2]), 100)
datamap = countmap(data)
bar((x -> datamap[x]).(s), xticks=(1:4, s))
Out[22]:

Hisogram plot 2d

In [23]:
x = randn(50000);
y = x * 1.2 + randn(50000);

plot(
    x,
    y,
    seriestype = :histogram2d,
    c = :vik,
    nbins = 200,
    show_empty_bins = :true
)

# equivalent
# histogram2d(
#     x,
#     y,
#     c = :vik,
#     nbins = 200,
#     show_empty_bins = :true
# )
Out[23]:

Box plot

Box plot (1)

In [24]:
using StatsPlots, Statistics, Random
Random.seed!(2018)

n = 30
science = rand(1:10, n)
@show median(science)
boxplot(science, label=permutedims(["science"]))
median(science) = 5.5
┌ 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])
└ @ Plots /home/jovyan/.julia/packages/Plots/Zuo5g/src/args.jl:1607
Out[24]:

Box plot (2)

In [25]:
# This outputs BoundsError. This is a bug of StatPlots.jl.
# Boxplot and violin broken - https://github.com/JuliaPlots/StatPlots.jl/issues/198
using StatsPlots, Statistics, Random
Random.seed!(2018)

n = 30
science = rand(1:10, n)
english = rand(1:10, n)

@show median(science)
@show median(english)

boxplot([science english], label=["science" "english"])
median(science) = 5.5
median(english) = 6.0
Out[25]:

Heatmap

In [26]:
using Random
Random.seed!(2018)

a = rand(5,5)
xlabel = string.(collect('A':'E'))
ylabel = string.(collect('a':'e'))
heatmap(a, xticks=(1:5, xlabel), yticks=(1:5, ylabel),
    fill_z=a, aspect_ratio=:equal)

fontsize = 15
nrow, ncol = size(a)
ann = [(i,j, text(round(a[i,j], digits=2), fontsize, :white, :center))
            for i in 1:nrow for j in 1:ncol]
annotate!(ann, linecolor=:white)
Out[26]:

Pie

In [27]:
# ref https://docs.juliaplots.org/latest/generated/pyplot/
x = ["Nerds","Hackers","Scientists"]
y = [0.4,0.35,0.25]
pie(x, y, title="The Julia Community",l = 0.5)
Out[27]:

Contour plot

Contour plot

In [28]:
f(x,y) = x^2 + y^2
x = repeat(range(0, stop=2, length=50), 1, 50)
y = repeat(range(0, stop=2, length=50), 1, 50)'

contour( f.(x, y) )
Out[28]:

Function Contour plot

In [29]:
# ref https://jp.mathworks.com/matlabcentral/fileexchange/35283-matlab-plot-gallery-function-contour-plot?focused=7851904&tab=example
f(x,y) = sin(3x) * cos(x+y)

x = 0:0.01:5
y = 0:0.01:5
contour(x, y, f, xlabel="x", ylabel="y", fill=true)
Out[29]: