Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions docs/src/UnitfulExt/unitfulext_examples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ plot!(rand(10)*u"m", inset=bbox(0.5, 0.5, 0.3, 0.3), subplot=2)

plot(y, ylabel="mass")

# Unless you want it untouched, in which case you can use a "protected" string using the `@P_str` macro.
# If you want it untouched, set the `yunitformat` to `:nounit`.
# In Plots v2, `:none` and `false` will also have this behavior.

plot(y, ylabel=P"mass in kilograms")
plot(y, ylabel="mass in kilograms", yunitformat=:nounit)

# Just like with the `label` keyword for legends, no axis label is added if you specify the axis label to be an empty string.

Expand All @@ -63,22 +64,19 @@ plot([plot(y, ylab="mass", title=repr(s), unitformat=s) for s in (nothing, true,

# `unitformat` can be one of a number of predefined symbols, defined in

URsymbols = if isdefined(Base, :get_extension)
getproperty(Base.get_extension(Plots, :UnitfulExt), :UNIT_FORMATS)
else
Plots.UnitfulExt.UNIT_FORMATS
end |> keys
## TODO: this is moved in v2
URsymbols = keys(Plots.UNIT_FORMATS)

# which correspond to these unit formats:

plot([plot(y, ylab="mass", title=repr(s), unitformat=s) for s in URsymbols]...)
plot([plot(y, ylab="mass", title=repr(s), unitformat=s) for s in URsymbols]..., size=(800, 600))

# `unitformat` can also be a `Char`, a `String`, or a `Tuple` (of `Char`s or
# `String`s), which will be inserted around the label and unit depending on the
# length of the tuple:

URtuples = [", in ", (", in (", ")"), ("[", "] = (", ")"), ':', ('$', '$'), (':', ':', ':')]
plot([plot(y, ylab="mass", title=repr(s), unitformat=s) for s in URtuples]...)
plot([plot(y, ylab="mass", title=repr(s), unitformat=s) for s in URtuples]..., size=(600, 600))

# For *extreme* customizability, you can also supply a function that turns two
# arguments (label, unit) into a string:
Expand All @@ -88,10 +86,17 @@ plot(y, ylab="mass", unitformat=formatter)

# ## Axis unit

# You can use the axis-specific keyword arguments to convert units on the fly
# You can use the axis-specific keyword arguments to choose axis units. However, doing this
# after the first series is plotted will produce incorrect plots--units get stripped according to the
# current units for each axis. So, this works:

plot(y, yunit=u"g")

# This will be wrong:

plot(y)
plot!(2y, yunit=u"g")

# ## Axis limits and ticks

# Setting the axis limits and ticks can be done with units
Expand Down Expand Up @@ -129,6 +134,11 @@ plot(x, y, z)

heatmap((1:5)u"μs", 1:4, rand(5,4)u"m", clims=(0u"m", 2u"m"))

# To specify colorbar units and unit formatting, use `zunit`, `zunitformat`,
# and `cbar_title`:

heatmap((1:5)u"μs", 1:4, rand(5,4)u"m", zunit=u"cm", zunitformat=:square, cbar_title="dist")

# ## Scatter plots

# You can do scatter plots
Expand Down
8 changes: 7 additions & 1 deletion docs/src/UnitfulExt/unitfulext_plots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ plot(rand(100)*u"km", layout=@layout([a b;c]), group=group, linetype=[:bar :scat
xs = [string("x", i) for i = 1:10]
ys = [string("y", i) for i = 1:4]
z = float((1:4) * reshape(1:10, 1, :)) * u"km"
heatmap(xs, ys, z, aspect_ratio=1)
heatmap(xs, ys, z, aspect_ratio=1, colorbar_title="dist", zunitformat=:square)

# ## Magic grid argument

Expand Down Expand Up @@ -193,4 +193,10 @@ p1 = plot(x, y, line_z=t, linewidth=3, legend=false)
p2 = scatter(x, y, marker_z=z, color=:bluesreds, legend=false)
plot(p1, p2)

# ## Shared axes

x = range(0.0u"s", 10.0u"s", length=21)
y = x * 5u"m/s" .+ 1u"m"
pl = plot(x, y)
pl2 = twinx()
plot!(pl2, x, 1 ./y, ylabel="inverse distance")
Loading