Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrap multiple plots together in a single image

I am trying to wrap numerous plots together, as they are closely related (showing density using 1 continuous and 1 categorical variable, broken down by day of the week, where each day is a different plot). In R, I can either use grid.arrange() from gridExtra or facet_wrap to wrap visualizations together to return to the user as 1 variable and image containing all plots. It looks like this:

All 7 of these plots are combined into 1 image, using <code>grid.arrange()</code>

How do I do this in Python?

like image 616
SRVFan Avatar asked Oct 21 '25 04:10

SRVFan


2 Answers

Since you allude to having used ggplot2 and facet_grid()/facet_wrap(), you should check out plotnine. From the homepage:

(ggplot(mtcars, aes('wt', 'mpg', color='factor(gear)'))
 + geom_point()
 + stat_smooth(method='lm')
 + facet_wrap('~gear'))

enter image description here

I was a long time R user and frankly found the python plotting landscape to be sort of a mess. I love being able to use plotnine to leverage past R experience vs. re-learning the wheel.

Now, the caveat is I came to this answer looking for a true grid.arrange(), as facets aren't as flexible. I want 6 mini-plots one one side and a true separate plot on the canvas on the other. This won't do that for me, but figured I'd add an answer here anyway while swinging by :)

like image 73
Hendy Avatar answered Oct 22 '25 18:10

Hendy


Yes, this can be done using matplotlib subplots. See this example.

The layout of the visualization can be initialized with the desired number of rows and columns. Each cell will contain a different subplot.

fig, axes = plt.subplots(nrows=3, ncols=2, figsize=(7, 7))
like image 43
jspcal Avatar answered Oct 22 '25 16:10

jspcal