I wanted some convenience in plotting several lines into one plot or several subplots.
Since Plots.jl does not yet serve the Makie backend, I wrote the functions below.
My first experiments with Makie, did I waste a lot of time for no reason, or can you recommend changes?
makie_plots: multi-line plot
using GLMakie
GLMakie.activate!()
function makie_plots(x, y; legends = [], title = [], xlabel = [], ylabel = [])
m = size(y, 2)
if isempty(legends)
legends = ["y$i" for i=1:m]
end
# standard colors from matplotlib
colors = ["#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd", "#8c564b", "#e377c2", "#7f7f7f", "#bcbd22", "#17becf"];
fig = Figure()
ax = Axis(fig[1, 1])
for i = 1:m
lines!(ax, x, y[:,i], label = legends[i], color = colors[(i-1)%10+1])
end
axislegend(ax)
if !isempty(title)
ax.title = title
# Bug? squeezes plots to width of title
#Label(fig[0, 1], title, textsize = 30)
end
if !isempty(xlabel)
ax.xlabel = xlabel
end
if !isempty(ylabel)
ax.ylabel = ylabel
end
fig
end
n = 100; m = 3
x = 1:n
y = randn(n,m)
#makie_plots(x, y)
#makie_plots(x, y, legends = ["toto", "titi", "tata"])
#makie_plots(x, y2, legends = ["toto", "titi", "tata"], title = "A Title")
makie_plots(x, y, legends = ["toto", "titi", "tata"], title = "A Title", xlabel = "Time [s]", ylabel = "Volts [V]")
makie_subplots: tightly aligned subplots with linked x-axis
using GLMakie
GLMakie.activate!()
function makie_subplots(x, y; legends = [], title = [], xlabel = [], ylabels = [])
m = size(y, 2)
if isempty(legends)
legends = ["y$i" for i=1:m]
end
# standard colors from matplotlib
colors = ["#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd", "#8c564b", "#e377c2", "#7f7f7f", "#bcbd22", "#17becf"];
fig = Figure()
ax = []
for i = 1:m
push!(ax, Axis(fig[i, 1]))
linkxaxes!(ax[1], ax[i])
lines!(ax[i], x, y[:,i], label = legends[i], color = colors[(i-1)%10+1])
axislegend(ax[i])
end
if !isempty(title)
ax[1].title = title
# Bug? squeezes plots to width of title
#Label(fig[0, 1], title, textsize = 30)
end
if !isempty(xlabel)
ax[end].xlabel = xlabel
end
hidexdecorations!.(ax[1:m-1], grid=false)
rowgap!(fig.layout,0)
fig
end
n = 100; m = 3
x = 1:n
y = randn(n,m)
#makie_subplots(x, y)
#makie_subplots(x, y, legends = ["toto", "titi", "tata"])
#makie_subplots(x, y, legends = ["toto", "titi", "tata"], title = "A Title")
makie_subplots(x, y, legends = ["toto", "titi", "tata"], title = "A Title", xlabel = "Time [s]")
makie_addsubplot: incrementally build tightly aligned subplots with linked x-axis
using GLMakie
GLMakie.activate!()
function makie_addsubplot(x, y; varargs...)
makie_addsubplot([], x, y; varargs...)
end
function makie_addsubplot(fap, x, y; legends = [], title = [], xlabel = [], ylabels = [])
if isempty(fap)
fig = Figure()
ax = []
pl = []
else
fig, ax, pl = fap
end
m = size(y, 2)
if isempty(legends)
legends = ["y$i" for i=1:m]
end
n = length(ax) + 1
push!(ax, Axis(fig[n, 1]))
linkxaxes!(ax...)
# standard colors from matplotlib
colors = ["#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd", "#8c564b", "#e377c2", "#7f7f7f", "#bcbd22", "#17becf"];
for i = 1:m
lines!(ax[n], x, y[:,i], label = legends[i], color = colors[(i-1)%10+1])
end
axislegend(ax[n])
if !isempty(title)
ax[1].title = title
end
if !isempty(xlabel)
ax[end].xlabel = xlabel
end
hidexdecorations!.(ax[1:n-1], grid=false)
rowgap!(fig.layout,0)
display(fig)
fap = (fig, ax, pl)
end
n = 100; m = 3
x = 1:n
y = randn(n,m)
fap = makie_addsubplot(x, cumsum(y[:,1]), legends = ["cumsum(toto)"])
makie_addsubplot(fap, x, y, legends = ["toto", "titi", "tata"])
makie_addsubplot(fap, x, y.^2, legends = ["toto2", "titi2", "tata2"], title = "A Title", xlabel = "Time [s]")