question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to draw subplots?

See original GitHub issue

plotnine is made using matplotlib as the back-end, so I’m guessing there must be a way to draw subplots (without using faceting).

Is there a way to do so? I’d be happy to contribute to the documentation if someone points out a solution.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:8
  • Comments:25 (11 by maintainers)

github_iconTop GitHub Comments

9reactions
peacejcommented, Oct 29, 2020

Such a feature would be hugely helpful, given how much people use cowplot/gridExtra for R ggplot2. I understand that it’s hard…

7reactions
ponnhidecommented, Jul 20, 2022

Please let me report here. I have now updated patchworklib to support plotnine v0.9.0 and the inheritance of the plotnine theme (The previous version of patchworklib cannot handle style settings of plotnine properly). Now, you can freely arrange multiple plotnine plots using patchworklib as follows.

import patchworklib as pw
from plotnine import *
from plotnine.data import *
g1 = pw.load_ggplot(ggplot(mtcars) 
                    + geom_point(aes("mpg", "disp"))
                    + theme(text=element_text(size=14), axis_title=element_text(size=18)),
                    figsize=(2,3))
g2 = pw.load_ggplot(ggplot(mtcars) 
                    + geom_boxplot(aes("gear", "disp", group="gear"))
                    + theme(text=element_text(size=14), axis_title=element_text(size=18)), 
                    figsize=(2,3))
g3 = pw.load_ggplot(ggplot(mtcars, aes('wt', 'mpg', color='factor(gear)')) 
                    + geom_point() + stat_smooth(method='lm') 
                    + facet_wrap('~gear')
                    + theme(text=element_text(size=14), axis_title=element_text(size=18)), 
                    figsize=(3,3))
g4 = pw.load_ggplot(ggplot(data=diamonds) 
                    + geom_bar(mapping=aes(x="cut", fill="clarity"), position="dodge")
                    + theme(text=element_text(size=14), axis_title=element_text(size=20, color="blue"), 
                            legend_text=element_text(size=14), legend_title=element_text(size=18)), 
                    figsize=(5,2))
g1234 = (g1|g2|g3)/g4
g1234.savefig()

Furthermore, by using the newest version of patchworklib and plotnine, you can draw a scatter plot with marginal distributions as follows.

import patchworklib as pw
from plotnine import *
from plotnine.data import *

g1 = pw.load_ggplot(ggplot(mpg, aes(x='cty', color='drv', fill='drv')) +
                    geom_density(aes(y=after_stat('count')), alpha=0.1) +
                    scale_color_discrete(guide=False) +
                    theme(axis_ticks_major_x=element_blank(),
                          axis_text_x =element_blank(),
                          axis_title_x=element_blank(),
                          axis_text_y =element_text(size=12),
                          axis_title_y=element_text(size=14),
                          legend_position="none"),
                    figsize=(4,1))

g2 = pw.load_ggplot(ggplot(mpg, aes(x='hwy', color='drv', fill='drv')) +
                    geom_density(aes(y=after_stat('count')), alpha=0.1) +
                    coord_flip() +
                    theme(axis_ticks_major_y=element_blank(),
                          axis_text_y =element_blank(),
                          axis_title_y=element_blank(),
                          axis_text_x =element_text(size=12),
                          axis_title_x=element_text(size=14)
                         ),
                    figsize=(1,4))

g3 = pw.load_ggplot(ggplot(mpg) +
                    geom_point(aes(x="cty", y="hwy", color="drv")) +
                    scale_color_discrete(guide=False) +
                    theme(axis_text =element_text(size=12),
                          axis_title=element_text(size=14)
                         ),
                    figsize=(4,4))

pw.param["margin"] = 0.2
(g1/(g3|g2)[g3]).savefig() #By specifying g3 in (g3|g2), g1 is positioned exactly on g3. 

If you face any problems, please let me know by creating issues on Github of patchworklib.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Creating multiple subplots using plt.subplots - Matplotlib
pyplot.subplots creates a figure and a grid of subplots with a single call, while providing reasonable control over how the individual plots are...
Read more >
Matplotlib Subplot - W3Schools
The subplot() function takes three arguments that describes the layout of the figure. The layout is organized in rows and columns, which are...
Read more >
17. Creating Subplots in Matplotlib | Numerical Programming
The function subplot create a figure and a set of subplots. It is a wrapper function to make it convenient to create common...
Read more >
Matplotlib Subplots - How to create multiple plots in same ...
1. Basic Overview · 2. Create a plot inside another plot using axes() · 3. Explicitly create the figure and then do add_axes()...
Read more >
Multiple Subplots | Python Data Science Handbook
Rather than creating a single subplot, this function creates a full grid of subplots in a single line, returning them in a NumPy...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found