Plot in even grid layout when the number of subplots is prime (using Plots.jl)

Hi there!

I’m using Plots.jl and I’m trying to customize a layout. I want to draw some plots in a grid layout, kind of like they are arranged when I don’t specify a layout at all.

Say we have the following situation:

	hs = [histogram(rand(100)) for _ in 1:13]
	plot(hs..., fmt=:png)

This results in this plot:

Now I’d like to plot these with only three columns. Note that the total number of plots is prime, i.e. there is no way to generate a grid(n,m) such that the number of grid cells match, I’ll always run into this n != n_override problem. But what I want is to have an evenly spaced grid with some plots missing at the end.

How to do this? I couldn’t figure it out. It must works since the default layout does kind of the same thing, only with a number of columns that doesn’t fit my needs.


There should be a cleaner way to do this, but if you don’t mind some hackery, you can just insert blank plots.

	hs = [histogram(rand(100)) for _ in 1:13]
	ncols = 3
	nrows = cld(length(hs), ncols)
	blankplot = plot(legend=false,grid=false,foreground_color_subplot=:white)
	for i = (length(hs)+1):(nrows*ncols)
		push!(hs, deepcopy(blankplot))
	plot(hs..., fmt=:png, layout=grid(nrows, ncols))

Cool, I don’t mind a bit of a hack. :slight_smile: