-
-
Notifications
You must be signed in to change notification settings - Fork 387
Expand file tree
/
Copy pathunitfulext_plots.jl
More file actions
199 lines (142 loc) · 6.38 KB
/
unitfulext_plots.jl
File metadata and controls
199 lines (142 loc) · 6.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#---------------------------------------------------------
# # [Plots.jl examples](@id 2_Plots)
#---------------------------------------------------------
#md # !!! note
#md # These examples are available as Jupyter notebooks.
#md # You can execute them online with [binder](https://mybinder.org/) or just view them with [nbviewer](https://nbviewer.jupyter.org/) by clicking on the badges above!
# These examples were slightly modified from some of [the examples in the Plots.jl documentation](https://github.com/JuliaPlots/Plots.jl/blob/master/src/examples.jl) 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(PlotsBase.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], PlotsBase.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"),
link = :all
)
# ## 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"),
link = :all
)
# ## Marker types
markers = intersect(PlotsBase.Commons._shape_keys, PlotsBase.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)
# ## Adding to subplots
plot(PlotsBase.fakedata(100, 10) * u"km", layout = 4, palette = [:grays :blues :heat :lightrainbow], bg_inside = [:orange :pink :darkblue :black])
# ## Contour plots
x = (1:0.05:10) * u"m"
y = (1:0.01:2) * u"s"
f(x, y) = x^2 / y
z = f.(x', y)
p1 = contour(x, y, f, fill = true)
p2 = contour(x, y, z)
p3 = contourf(x, y, z)
plot(p1, p2, p3)
# ## 3D
n = 100
ts = range(0, stop = 8π, length = n) * u"rad"
x = @. ts * cos(ts)
y = @. 0.1ts * sin(ts)
z = ts
plot(x, y, z, zcolor = reverse(z), m = (10, 0.8, :blues, Plots.stroke(0)), leg = false, cbar = true, w = 5, xlabel = "x", ylabel = "y", zlabel = "z")
plot!(zeros(n), zeros(n), z, w = 5)
# ## Groups and Subplots
group = rand(map((i -> "group $(i)"), 1:4), 100)
plot(rand(100) * u"km", layout = @layout([a b;c]), group = group, linetype = [:bar :scatter :steppre], linecolor = :match)
# ## Heatmap, categorical axes, and aspect_ratio
xs = [string("x", i) for i in 1:10]
ys = [string("y", i) for i in 1:4]
z = float((1:4) * reshape(1:10, 1, :)) * u"km"
heatmap(xs, ys, z, aspect_ratio = 1, colorbar_title = "dist", zunitformat = :square)
# ## Magic grid argument
x = rand(10) * u"km"
p1 = plot(x, title = "Default looks")
p2 = plot(x, grid = (:y, :olivedrab, :dot, 1, 0.9), title = "Modified y grid")
p3 = plot(deepcopy(p2), title = "Add x grid")
xgrid!(p3, :on, :cadetblue, 2, :dashdot, 0.4)
plot(p1, p2, p3, layout = (1, 3), label = "", fillrange = 0, fillalpha = 0.3)
# ## Framestyle
# Suggestion: we might want to not add the unit label when the axis is not shown?
scatter(fill(randn(10), 6) * u"m", fill(randn(10), 6) * u"s", framestyle = [:box :semi :origin :zerolines :grid :none], title = [":box" ":semi" ":origin" ":zerolines" ":grid" ":none"], color = permutedims(1:6), layout = 6, label = "", markerstrokewidth = 0, ticks = -2:2)
# ## Lines and markers with varying colors
# note that marker_z as a function did not work so it is modified here
t = range(0, stop = 1, length = 100) * u"s"
θ = 6π * u"rad/s" * t
x = @. t * cos(θ)
y = @. t * sin(θ)
z = x + y
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")