问题
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:
How do I do this in Python?
回答1:
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))
回答2:
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'))
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 :)
来源:https://stackoverflow.com/questions/50086651/wrap-multiple-plots-together-in-a-single-image