Plots.jl: layout of several plots, with more than one curve per plot

1,661 views
Skip to first unread message

Ferran Mazzanti

unread,
Nov 8, 2016, 4:20:39 AM11/8/16
to julia-users
Hi,

I'm gathering interest in Plots.jl in order to make complex plotting structures. Just as an example, I have a set of data (called y) and some operations performed on it, stored in arrays of obvious names y2, logy, expy etc...

I have managed to create something that displays one curve per plot

lay = @layout [  a{0.4w} grid(2,2) ]
plot(
[y y2 sqrty logy expy],
layout = lay,
grid   = [true false false false false],
title = ["y" "y^2" "sqrt(y)" "log(y)" "exp(y)"],titleloc="center",titlefont=font(12),
xlims = [(0,12)  (0,10) (0,20) (0,20) (0,12) ],
)

which puts one curve in each plot. What would be the needed modifications here in order to
plot y and cosy in the first plot, and sqrty and siny on the second plot?

Thanks for your help,

Ferran.

Scott T

unread,
Nov 8, 2016, 7:22:20 AM11/8/16
to julia-users
You can break down the plot command into chunks and then call plot on those chunks to build up a plot from several pieces. In this way, you can make individual plots with multiple series. Then you can combine them according to your desired layout.

This isn't the best place to post a full example so I put a notebook up for you to have a look at here: 

Cheers,
Scott

Ferran Mazzanti

unread,
Nov 8, 2016, 11:18:20 AM11/8/16
to julia-users
Oh!
Scott you're very kind to take your time helping me with the notebook... thanks a lot. I'll take a careful look and report back if I find problems.
Best,
Ferran.

Tom Breloff

unread,
Nov 8, 2016, 11:28:11 AM11/8/16
to julia-users
Thanks for putting those examples together Scott.  Do you want to contribute that to ExamplePlots?

Ferran: as Scott has shown nicely, there's an ton of different ways to do what you want, and the "right" way is going to depend on your problem... what the layouts/data look like.  Many times it's easiest to build the plots independently and then just do: plot(p1, p2, p3, p4, p5, layout = ...)

Scott T

unread,
Nov 8, 2016, 12:00:34 PM11/8/16
to julia-users
Sure, happy to add to ExamplePlots! Do you have any more suggestions for layout-related features you'd like to showcase? Inset plots? I think the key things to get across are how to define layouts and how to overlay/facet series within a layout.

Scott

Tom Breloff

unread,
Nov 8, 2016, 12:08:43 PM11/8/16
to julia-users
Even better... you should add a tutorial for how to do: https://github.com/tbreloff/Plots.jl/issues/541

Ferran Mazzanti

unread,
Nov 11, 2016, 6:05:46 AM11/11/16
to julia-users
Sorry for sneaking in again, but I have tried to extrapolate the examples in the notebook (thanks again Scott) to include a fifth plot below (that is, to get the same structure but with an additional plot at the bottom, spanning the whole width and a smaller height) and fail. I can get it to work when the upper-right grid, which now is 2x2, is reduced to 1x2, but otherwise I can only get a large plot with a smaller one inside...
Is there a (proper) way to get this done?
Thanks again,
Ferran,

Tom Breloff

unread,
Nov 11, 2016, 6:10:02 AM11/11/16
to julia...@googlegroups.com
"@layout [a grid(2,2); b]" should work

Ferran Mazzanti

unread,
Nov 14, 2016, 3:35:03 AM11/14/16
to julia-users
Hi again,

thanks Scott. That doesn't work on my ubuntu machine. Looks like this is too complex a plot, and what I get is a big plot with a single subplot on it. That's whay I was asking for help, actually... If I reduce the grid to 2x1 (so putting 2 plots instead of 4 in the grid), things work well...
So shall I understand there is some sort of bug in the codem, then?

Best,

Ferran.

Patrick Kofod Mogensen

unread,
Nov 14, 2016, 4:19:42 AM11/14/16
to julia-users
Hi Ferran,

First of all, it is so much easier for people to help you if you post the code you don't understand isn't working.

Best,
Patrick

Ferran Mazzanti

unread,
Nov 14, 2016, 6:29:09 AM11/14/16
to julia-users
It is a minor variation of the example given by Scott
for some data set y, cosy, y2, sqrty, siny, logy (doesn't matter the values, could be random)

plot_1 = plot([y cosy],
    title  = "Data y",
    xlims  = (0,10),
    ylims  = (-0.1,1.1),
    grid   = true,
    xlabel = "Iteration",
    ylabel = "y & cos(y)"
);

plot_2 = plot(y2,
    title  = "Data y Squared",
    xlims  = (0,10),
    ylims  = (-0.1,0.5),
    grid   = false,
    xlabel = "Iteration",
    ylabel = "y^2",
    legend = false,
);

plot_3 = plot( [sqrty siny],
title  = "Square Root of y",
    xlims  = (0,10),
    ylims  = (-0.1,0.5),
    grid   = false,
    xlabel = "Iteration",
    ylabel = "y^2"
);

plot_4 = plot(logy,
title  = "Log of y",
    xlims  = (0,10),
    ylims  = (-10,0),
    grid   = false,
    xlabel = "Iteration",
    ylabel = "y^2"
);

plot_5 = plot(expy,
title  = "Exp of y",
    xlims  = (0,10),
    ylims  = (1,2),
    grid   = false,
    xlabel = "Iteration",
    ylabel = "y^2"
);

lay = @layout [a grid(2,2); b]
plot( plot_1, plot_2, plot_3, plot_4, plot_5, plot_5, layout = lay )

Scott T

unread,
Nov 14, 2016, 6:59:45 AM11/14/16
to julia-users
The layout has space for 6 plots but the final plot command only supplies 5. When I run your example (on the development branch of Plots) I get an error because of that. Have you tried the dev branch? `Pkg.checkout("Plots, "dev")`, restart julia and re-run it.

Scott

Scott T

unread,
Nov 14, 2016, 7:03:19 AM11/14/16
to julia-users
Oh my mistake, I see you have supplied plot_5 twice. If I do that, I get the problem you describe.

I'm not quite sure why you'd want to repeat a plot, but it looks like this is causing problems. If you really want to include plot_5 twice, I suggest making a new plot_6 with the same parameters and including that instead.

I'll open a bug report at Plots for this behaviour.

Scott

Tom Breloff

unread,
Nov 14, 2016, 8:50:13 AM11/14/16
to julia...@googlegroups.com
The behavior is currently undefined if you pass in the same plot twice. Unless there's a compelling reason, I don't think that will change. 

Ferran Mazzanti

unread,
Nov 15, 2016, 4:16:53 AM11/15/16
to julia-users
Oh, now I see that just by copying plot_5 with a new name plot_6 and replacing

plot( plot_1, plot_2, plot_3, plot_4, plot_5, plot_5, layout = lay )

with

plot( plot_1, plot_2, plot_3, plot_4, plot_5, plot_6, layout = lay ),

it works. But IT IS a bug, there is no reason why should not be able to repeat a plot. You are supposed to be able to plot whatever you want, isn't it? And if you think it makes not sense, then at least one should document it. I could think of many situations where the same plot is repeated... for instance, when learning how to arrange several plots together, and you do not worry about what you plot, just want to see the final arrangement. But if you still think it does not make sense what I say (why not?), it doesn't hurt to document the limitations...

Cheers,

Ferran.

Tom Breloff

unread,
Nov 15, 2016, 6:41:54 AM11/15/16
to julia...@googlegroups.com
Serious "give a mouse a cookie syndrome".  You can do what you want by calling 'plot_6 = deepcopy(plot_5)' first. 

Ferran Mazzanti

unread,
Nov 16, 2016, 5:39:04 AM11/16/16
to julia-users
That's not the point but it doesn't really matter :)

Scott T

unread,
Nov 16, 2016, 7:27:08 AM11/16/16
to julia-users
Hi Tom, just to let you know that I'm putting together some subplot demos for ExamplePlots.jl. I was hoping to show some examples of linking axes, but I got a little overambitious and it's not quite the result I intended. I have popped the question on Discourse if you wanted to take a look. 

Scott
Reply all
Reply to author
Forward
0 new messages