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:

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

This results in this plot:
image

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.

Thanks!

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

let
	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))
	end
	plot(hs..., fmt=:png, layout=grid(nrows, ncols))
end

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