# Plots.jl examples

Note

These examples are available as Jupyter notebooks. You can execute them online with binder or just view them with nbviewer by clicking on the badges above!

These examples were slightly modified from some of the examples in the Plots.jl documentation and can be used as both a tutorial or as a series of test for Unitful recipes. (they are essentially the same except we have added some units to the data).

First we need to tell Julia we are using Unitful and Plots

using Unitful, Plots

## Lines

plot(Plots.fakedata(50, 5) * u"m", w=3)

## Parametric plots

plot(t -> sin(t)*u"s", t -> sin(2t)*u"m", 0, 2π, line=4, leg=false, fill=(0, :orange))

## Colors

y = rand(100)*u"km"
plot((0:10:100)*u"hr", rand(11, 4)*u"km", lab="lines", w=3, palette=:grays, fill=0, α=0.6)
scatter!(y, zcolor=abs.(y .- 0.5u"km"), m=(:heat, 0.8, Plots.stroke(1, :green)), ms=10 * abs.(y .- 0.5u"km") .+ 4u"km", lab="grad")

## Global

Note that a few changes had to be made for this to work.

using Statistics
y = rand(20, 3)*u"W"
x = (1:size(y,1))*u"Hz"
plot(x, y, xlabel="XLABEL", xlims=(-5, 30), xflip=true, xticks=0:2:20, background_color=RGB(0.2, 0.2, 0.2), leg=false)
hline!(mean(y, dims=1) + rand(1, 3)*u"W", line=(4, :dash, 0.6, [:lightgreen :green :darkgreen]))
vline!([5, 10]*u"Hz")
title!("TITLE")
yaxis!("YLABEL", :log10)

## Arguments

ys = Vector[rand(10), rand(20)] .* u"km"
plot(ys, color=[:black :orange], line=(:dot, 4), marker=([:hex :d], 12, 0.8, Plots.stroke(3, :gray)))

## Build plot in pieces

plot(rand(100) / 3 * u"km", reg=true, fill=(0, :green))
scatter!(rand(100) * u"km", markersize=6, c=:orange)

## Histogram2D

histogram2d(randn(10000) * u"cm", randn(10000) * u"cm", nbins=20)

## Line types

linetypes = [:path :steppre :steppost :sticks :scatter]
n = length(linetypes)
x = Vector[sort(rand(20)) for i = 1:n] * u"km"
y = rand(20, n) * u"ms"
plot(x, y, line=(linetypes, 3), lab=map(string, linetypes), ms=15)

## Line styles

styles = intersect([:solid, :dash, :dot, :dashdot, :dashdotdot], Plots.supported_styles())
styles = reshape(styles, 1, length(styles))
n = length(styles)
y = cumsum(randn(20, n), dims=1) * u"km"
plot(y, line=(5, styles), label=map(string, styles), legendtitle="linestyle")

## Ribbons

Ribbons can be added to lines via the ribbon keyword; you can pass:

• an array (for symmetric ribbons)
• a function
• a number

(Tuple of arrays for upper and lower bounds are currently unsupported.)

x = y = (0:10)*u"m"
plot(
plot(x,y; ribbon = (0:0.5:5)*u"m", label = "Vector"),
plot(x,y; ribbon = sqrt, label = "Function"),
plot(x,y; ribbon = 1u"m", label = "Constant"),
)

## Fillrange

The fillrange keyword defines a second line and fills between it and the y data. Note: ribbons are fillranges.

x = y = (0:10)*u"m"
plot(
plot(x,y; fillrange = (0:0.5:5)*u"m", label = "Vector"),
plot(x,y; fillrange = sin, label = "Function"),
plot(x,y; fillrange = 0u"m", label = "Constant"),
)

## Marker types

markers = intersect(Plots._shape_keys, Plots.supported_markers())
markers = reshape(markers, 1, length(markers))
n = length(markers)
x = (range(0, stop=10, length=n + 2))[2:end - 1] * u"km"
y = repeat(reshape(reverse(x), 1, :), n, 1)
scatter(x, y, m=(8, :auto), lab=map(string, markers), bg=:linen, xlim=(0, 10), ylim=(0, 10))

## Bar

bar(randn(99) * u"km")

## Histogram

histogram(randn(1000) * u"km", bins=:scott, weights=repeat(1:5, outer=200))

## Subplots

l = @layout([a{0.1h};b [c;d e]])
plot(randn(100, 5) * u"km", layout=l, t=[:line :histogram :scatter :steppre :bar], leg=false, ticks=nothing, border=:none)

plot(Plots.fakedata(100, 10) * u"km", layout=4, palette=[:grays :blues :heat :lightrainbow], bg_inside=[:orange :pink :darkblue :black])