Wrap multiple plots together in a single image

↘锁芯ラ 提交于 2021-02-18 18:20:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!