Patchworklib
Patchwork for matplotlib: A subplot manager for intuitive layouts in matplotlib and seaborn.
Install / Use
/learn @ponnhide/PatchworklibREADME
patchworklib
Patchworklib is a universal composer of matplotlib-related plots (simple matplotlib plots, Seaborn plots (both axis-level and figure-level), and plotnine plots). This library is inspired by patchwork for ggplot2. Accordingly, as original patchwork, users can easily align matplotlib plots with only "/" and "|" operators. Although a good subplot composer, "subplot_mosaic" is provided from matplotlib formally, I believe the way of patchworklib is more straightforward and more flexible.
Additionally, several third-party libraries based on matplotlib, such as plotnine and seaborn, provide functions to generate beautiful plots with simple python codes, but many of those plots cannot be handled as matplotlib subplots. Therefore, their placement must be adjusted manually. Now, scientists spend their valuable time arranging figures.
Patchworklib provides a solution for the problem. By using patchworklib, any kind of seaborn and plotnine plots can be handled as matplotlib subplots.
Join Our Team: Bioinformatics Researcher Wanted.
We are currently seeking a skilled researcher with expertise in bioinformatics to join our lab.
For more details and to apply, please visit the following URL.
Installation
For normal users, we recommended you to install the official release as follows.
pip install patchworklib
If you want to use developmental version, it can be installed using the following single command:
pip install git+https://github.com/ponnhide/patchworklib.git
News
10212025: version 0.6.6 is released.
- Dropped support of plotnine. Plotnine now implemented native plot composition.
- Fixed some bugs.
10122024: version 0.6.5 is released.
- Seaborn v0.13.X is now supported.
- plotnine v0.13.6 might be supported.
10232023: version 0.6.3 is released.
- Seaborn v0.13.0 is now supported.
- I have confirmed patchworklib still works for plotnine v0.12.3.
05162023: version 0.6.1 was released.
- I could not fully support plotnine version 0.12.1 (It works, but patchworklib arrangement results are not as expected). Maybe, I will do not support plotnine in the future.
- Bricks object gained the new methods
align_xlabelsandalign_ylabels, which help users align x/y labels of the given Brick object. - The new argument
equal_spacingwas added to the stack function. If this value isTrue, axes bounding-boxes should be placed with equal spacing between them. Otherwise, depending on the text values of x/y tick labels and x/y labels, they may not always be equally spaced. - Closed and maybe solved the issue "Align labels of Bricks when subplot axes are aligned. #40"
- Closed and maybe solved the issue "Doesn't work with Plotnine Seaborn Theme. #37"
- Closed and maybe solved the issue "The position of the title cannot be set in plotnine. #36"
08152022: Version 0.4.7 was released.
- A few bugs were fixed.
- Add inset function. please see the following example.
- Add
keep_aspectparameter tohstackandvstackfunciton. - Example codes were moved to patchworklib-examples
import patchworklib as pw
from plotnine import *
from plotnine.data import *
g1 = pw.load_ggplot(ggplot(mtcars) + geom_point(aes("mpg", "disp")),figsize=(4, 2))
g2 = pw.load_ggplot(ggplot(mtcars) + geom_boxplot(aes("gear", "disp", group = "gear")) + theme_classic())
g12 = pw.inset(g1,g2)
g12.savefig()
<img src="https://raw.githubusercontent.com/ponnhide/patchworklib/main/img/inset_plotnine.png" width="600x600">
g12 = pw.inset(g1,g2, loc="lower left", hratio=0.4, wratio=0.2)
g12.savefig("inset_plotnine2.png")
<img src="https://raw.githubusercontent.com/ponnhide/patchworklib/main/img/inset_plotnine2.png" width="600x600">
</details>
08092022: Version 0.4.6 was released.
- A few bugs were fixed (See issue #18).
- Plotting speed was improved.
07202022: Version 0.4.5 was released.
- A few bugs were fixed.
- Modified functions relating to the inheritance of the ggplot theme. If you use patchworklib for handling plotnine plots, please do update.
- When specifying a plot (Brick object) in Bricks object, you can specify the Brick object directly instead of the label name of the Brick object. Please see the following example.
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.
<img src="https://raw.githubusercontent.com/ponnhide/patchworklib/main/img/joint_plot_plotnine.png" width="800x800">
</details>
07192022: Version 0.4.3 was released.
- A few bugs were fixed.
basefigureparameter was added. You can access the base figure of patchworklib bypatchworklib.basefigure- plotnine v0.9.0 was now supported. Probably, it still have some bugs. If you find bugs, please let me know on the issue.
04222022: Version 0.4.2 was released.
- A few bugs were fixed.
04182022: Version 0.4.1 was released.
load_seaborngridcan accepts aseaborn.clustermapplot. For details, see example code on Google colab- A few bugs were fixed.
03272022: Version 0.4.0 was released.
- Add docstring for each method and class.
- Add several new methods of
patchworklib.Bricksclass to set common label, title, spine and colorbar forBrickobjets in theBricksobject.
For usage, please refer to the docstring or the example codes on Google colab.
02042022: Version 0.3.6 was released.
- A few bugs relating with the function to arrange multiple polar plot objects.
02042022: Version 0.3.5 was released.
- A few bugs in
move_legendwere fixed. (Themove_legendfor seaborn grided plot was not working properly.) - Improved the speed of
savefigoperation.
01242022: Version 0.3.3 was released.
- A few bugs were fixed.
01222022: Version 0.3.0 was released.
<details> <summary> Patchworklib now supports the function to arrange matplotlib.projections.polar.PolarAxes ojbects. </summary>When you load a matplotlib.projections.polar.PolarAxes object as a Brick class object, please use 'cBrick' instead of 'Brick'.
Now, you can arrange multiple circos plots using pycircos and patchworklib. Please see the following example code.
https://colab.research.google.com/drive/1tkn7pxRqh9By5rTFqRbVNDVws-o-ySz9?usp=sharing
01212022: Version 0.2.1 was released.
- A few bugs for 'load_seaborngrid' were fixed.
01202022: Version 0.2.0 was released.
<details> <summary> Patchworklib is now possible to arrange Seabron gridded plots. The "stack" function is added. A few bugs were fixed. </summary>Arranging seaborn gridded plots
Patchworklib supported the function to arange multiple seborn plots generated based on axisgrid (FacetGrid, PairGrid, and JointGrid). Let's see the follwoing example.
import os
import seaborn as sns
import patchworklib as pw
from functools import reduce
pw.overwrite_axisgrid() #When you use pw.load_seagorngrid, 'overwrite_axisgrid' should be executed.
df = sns.load_dataset("penguins
