SkillAgentSearch skills...

Patchworklib

Patchwork for matplotlib: A subplot manager for intuitive layouts in matplotlib and seaborn.

Install / Use

/learn @ponnhide/Patchworklib
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

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_xlabels and align_ylabels, which help users align x/y labels of the given Brick object.
  • The new argument equal_spacing was added to the stack function. If this value is True, 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"
<details> <summary> <h2> Change log </h2> </summary> #### 12082022: version 0.5.0 was released. - New operators, "+" and "-", were added. - plotnine > v0.10.x is now supported. - Plots generated by object-oriented seaborn interface can now be handled by patchworklib. - Descriptions of each function and class provided in patchworklib was added to this repository. If you want to know how to use patchworklib in detail, please see [API.md](https://github.com/ponnhide/patchworklib/blob/main/API.md). - Updated [patchworklib-examples](https://github.com/ponnhide/patchworklib-examples)

08152022: Version 0.4.7 was released.

  • A few bugs were fixed.
  • Add inset function. please see the following example.
  • Add keep_aspect parameter to hstack and vstack funciton.
  • Example codes were moved to patchworklib-examples
<details> <summary> Create an inset element </summary>
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.
<details> <summary> Alignment of a plotine plot by specifying a Brick object in the Bricks object. </summary>
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.
  • basefigure parameter was added. You can access the base figure of patchworklib by patchworklib.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_seaborngrid can accepts a seaborn.clustermap plot. 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.Bricks class to set common label, title, spine and colorbar for Brick objets in the Bricks object.
     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_legend were fixed. (The move_legend for seaborn grided plot was not working properly.)
  • Improved the speed of savefig operation.

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

</details>

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
View on GitHub
GitHub Stars434
CategoryDevelopment
Updated14d ago
Forks27

Languages

Jupyter Notebook

Security Score

100/100

Audited on Mar 14, 2026

No findings