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
using Pkg
Pkg.activate(".")
Activating project at `~/PlotsGallery`
# Pkg.update()
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
Pkg.precompile()
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
ENV["GKS_ENCODING"]="utf-8" # for scientific formatter
using Plots;
# pyplot()
gr()
Plots.GRBackend()
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))
# 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))
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)
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)
f(x) = 5exp(-x^2)
g(x) = x^2
plot([f, g], -3, 3)
xₜ(t) = sin(t)
yₜ(t) = sin(2t)
plot(xₜ, yₜ, 0, 2π, leg=false, fill=(0,:orange))
t = range(0, stop=10, length=1000)
x = cos.(t)
y = sin.(t)
z = sin.(5t)
plot(x, y, z)
plot(exp, -5, 5, yscale=:log10, title="semilogy")
plot(log, 0+0.00001, 5, xscale=:log10, title="semilogx")
f(x) = x^-1.7
plot(f, 1e-3, 3, scale=:log10, title="log-log")
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))
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"])
ticklabel = string.(collect('a':'l'))
bar(1:12, orientation=:h, yticks=(1:12, ticklabel), yflip=true)
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")
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))
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
# )
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
# 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
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)
# 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)
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) )
# 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)
# ref https://jp.mathworks.com/matlabcentral/fileexchange/35285-matlab-plot-gallery-function-polar-plot?focused=6793167&tab=example
r(θ) = 1 + cos(θ) * sin(θ)^2
plot(r, 0, 2π, proj=:polar, lims=(0,1.5))
r(θ) = 1 + cos(θ) * sin(θ)^2
θ = range(0, stop=2π, length=50)
plot(θ, r.(θ), proj=:polar, lims=(0,1.5))
using Random
Random.seed!(2018)
n = 24
R = rand(n+1)
θ = 0:2pi/n:2pi
plot(θ, R, proj=:polar, line=:steppre, lims=(0,1))
using Random
Random.seed!(2018)
n = 50
x = rand(n)
y = rand(n)
ms = rand(50) * 30
scatter(x, y, markersize=ms)
using Random
Random.seed!(2018)
n = 50
x = rand(n)
y = rand(n)
z = rand(n)
ms = rand(50) * 30
scatter(x, y, z, markersize=ms)
x = sin.(0:0.3:2pi)
plot(x, line=:stem, marker=:star, markersize=20)
x = sin.(0:0.3:2pi)
plot(x, line=:steppre, markersize=20)
using Random
Random.seed!(2018)
f(x) = 2 * x + 1
x = 0:0.1:2
n = length(x)
y = f.(x) + randn(n)
plot(x, y,
xerr=0.1 * rand(n),
yerr=rand(n))
# pyplot()
using Measures, Random
Random.seed!(2018)
plot(randn(100), ylabel="y1", leg=:topright)
plot!(twinx(), randn(100)*10,
c=:red,
ylabel="y2",
leg=:bottomright,
size=(600, 400))
plot!(right_bottom=10mm)
# gr()
a = plot(sin, 0, 2pi, xlabel="x1")
b = plot(cos, 0, 2pi, xlabel="x2")
c = histogram(randn(1000), xlabel="x3")
d = plot(x->exp(-x^2), -3, 3, xlabel="x4")
plot(a, b, c, d)
a = plot(sin, 0, 2pi, xlabel="x1")
b = plot(cos, 0, 2pi, xlabel="x2")
c = histogram(randn(1000), xlabel="x3")
d = plot(x->exp(-x^2), -3, 3, xlabel="x4")
plot(a, b, c, d, layout=(1,4), size=(1000, 300))
a = plot(sin, 0, 2pi, xlabel="x1")
b = plot(cos, 0, 2pi, xlabel="x2")
c = histogram(randn(1000), xlabel="x3")
d = plot(x->exp(-x^2), -3, 3, xlabel="x4")
plot(a, b, c, d, layout=(4,1), size=(300, 800))
n = 7
f(x,y) = 1 / hypot(x, y)
x = repeat(-3:(2*3)/n:3, 1, n) |> vec
y = repeat(-3:(2*3)/n:3, 1, n)' |> vec
vx = f.(x,y) .* cos.(atan.(y,x)) |> vec
vy = f.(x,y) .* sin.(atan.(y,x)) |> vec
quiver(x, y, quiver=(vx, vy), aspect_ratio=:equal)
f(x,y) = x^2 + y^2
x = -10:10
y = x
surface(x, y, f)
f(x,y) = x^2 + y^2
x = -10:10
y = x
plot(x, y, f, linetype=:surface)
f(x,y) = x^2 + y^2
x = -10:10
y = x
plot(x, y, f, linetype=:wireframe)
# ref https://jp.mathworks.com/matlabcentral/fileexchange/35229-matlab-plot-gallery-standard-line-colors?focused=6792895&tab=example
using SpecialFunctions
x = 0:0.2:10;
y0 = besselj.(0,x);
y1 = besselj.(1,x);
y2 = besselj.(2,x);
y3 = besselj.(3,x);
y4 = besselj.(4,x);
y5 = besselj.(5,x);
y6 = besselj.(6,x);
plot(x, y0, c=:red)
plot!(x, y1, c=:green)
plot!(x, y2, c=:blue)
plot!(x, y3, c=:cyan)
plot!(x, y4, c=:magenta)
plot!(x, y5, c=:yellow)
plot!(x, y6, c=:black)
Plots.supported_styles()
6-element Vector{Symbol}: :auto :solid :dash :dot :dashdot :dashdotdot
# ref https://jp.mathworks.com/matlabcentral/fileexchange/35230-matlab-plot-gallery-standard-line-styles?focused=6792901&tab=example
using SpecialFunctions
style = Plots.supported_styles()[2:end]
style = reshape(style, 1, length(style))
x = 0:0.2:10;
y0 = besselj.(0,x);
y1 = besselj.(1,x);
y2 = besselj.(2,x);
y3 = besselj.(3,x);
y4 = besselj.(4,x);
plot(x, [y0 y1 y2 y3 y4], line=(3, style))
# ref https://jp.mathworks.com/matlabcentral/fileexchange/35237-matlab-plot-gallery-adding-latex-to-plots?focused=6792946&tab=example
using LaTeXStrings, Measures
gr()
fib = zeros(12);
for i = 1:12
fib[i] = (((1+sqrt(5))/2)^i - ((1-sqrt(5))/2)^i)/sqrt(5);
end
plot(fib,
marker=:circle,
xlabel=L"n",
ylabel=L"F_n",
annotation=(5, 100, L"F_n = \frac{1}{\sqrt{5}} \left[\left( \frac{1+\sqrt{5}}{2} \right)^n - \left( \frac{1-\sqrt{5}}{2} \right)^n \right]"))
For pyplot backend
# ref https://jp.mathworks.com/matlabcentral/fileexchange/35237-matlab-plot-gallery-adding-latex-to-plots?focused=6792946&tab=example
using LaTeXStrings, Measures
pyplot()
Plots.PyPlot.rc("text", usetex ="true")
fib = zeros(12);
for i = 1:12
fib[i] = (((1+sqrt(5))/2)^i - ((1-sqrt(5))/2)^i)/sqrt(5);
end
plot(fib,
marker=:circle,
xlabel=L"n",
ylabel=L"F_n",
annotation=(5, 100, L"F_n = \frac{1}{\sqrt{5}} \left[\left( \frac{1+\sqrt{5}}{2} \right)^n - \left( \frac{1-\sqrt{5}}{2} \right)^n \right]")
)
┌ Warning: `vendor()` is deprecated, use `BLAS.get_config()` and inspect the output instead │ caller = npyinitialize() at numpy.jl:67 └ @ PyCall /home/jovyan/.julia/packages/PyCall/3fwVL/src/numpy.jl:67
using Measures
gr()
plot(sin, 0, 2π,
xticks=0:0.5:2π,
xrotation=60,
xtickfont=font(30),
bottom_margin=15mm)
plot(sin, 0, 2π, axis=false)
plot(sin, 0, 2π,
xtick=(0:0.5:2π, ["$i a" for i in 0:0.5:2π]),
ytick=-1:0.2:1,
xrotation=60, ## -> rotate xticks 60 degree
yrotation=90,
# rotation=60, # x,y ticks rotation
)
plot(exp, 0, 10, yformatter=:scientific)
plot(sin, 0, 2π, xlims=(-10, 10), ylims=(-2,2))
using Random
s = bitrand(10, 10)
heatmap(s,
aspect_ratio=:equal,
c=:blues,
colorbar=false)
plot(x->x, 0:0.01:2π,
proj=:polar,
xflip=true,
yflip=true,
# flip=true # x,y-flip
)
using Measures, LaTeXStrings
plot(sin, 0, 2π,
title=L"y = \sin(x)",
titlefont=font(40), ## title
xlabel=L"x",
ylabel="y",
xguidefont=font(30), ## x-label
yguidefont=font(20), ## x-label
# guidefont=font(20), # x,y-label
xtick=(0:0.5:2π, ["\$ $(i) \$" for i in 0:0.5:2π]),
ytick=-1:0.5:1,
xtickfont=font(15),
ytickfont=font(20),
# tickfont=font(10), # x,y-ticks font
label="Sin function",
legendfont=font(12),
xlims=(0,2π),
ylims=(-1,1),
bottom_margin=5mm,
left_margin=10mm,
top_margin=15mm
)
This notebook was generated using Literate.jl.