Fundamentals 4 min read

Combining Multiple Plots in R and Python Using patchwork and patchworklib

This tutorial explains how to merge multiple graphs into a single figure using the patchwork package in R and the patchworklib library in Python, providing installation steps, code examples for arranging plots side‑by‑side and in grids, and visual results.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Combining Multiple Plots in R and Python Using patchwork and patchworklib

Problem: How to combine several individual plots into one composite figure.

R solution (patchwork): Install the package and use the # install.packages("devtools") devtools::install_github("thomasp85/patchwork") command. Load the libraries and create plots with library(ggplot2) library(patchwork) p1 <- ggplot(mtcars) + geom_point(aes(mpg, disp)) p2 <- ggplot(mtcars) + geom_boxplot(aes(gear, disp, group = gear)) p1 + p2 to place two plots side by side. For more complex layouts, define additional plots and combine them, e.g., p3 <- ggplot(mtcars) + geom_smooth(aes(disp, qsec)) p4 <- ggplot(mtcars) + geom_bar(aes(carb)) (p1 | p2 | p3) / p4 , producing a two‑row layout with three plots on the first row and one on the second.

Python solution (patchworklib): Install the library with pip3 install patchworklib . Use it to arrange Matplotlib/Seaborn/plotnine figures. Example code: import patchworklib as pw import seaborn as sns fmri = sns.load_dataset("fmri") ax1 = pw.Brick(figsize=(3,2)) sns.lineplot(x="timepoint", y="signal", hue="region", style="event", data=fmri, ax=ax1) ax1.legend(bbox_to_anchor=(1.05, 1.0), loc='upper left') ax1.set_title("ax1") titanic = sns.load_dataset("titanic") ax2 = pw.Brick(figsize=(1,2)) sns.barplot(x="sex", y="survived", hue="class", data=titanic, ax=ax2) ax2.move_legend(new_loc='upper left', bbox_to_anchor=(1.05, 1.0)) ax2.set_title("ax2") ax12 = ax1 | ax2 ax12.savefig("ax12.png")

Further examples show how to combine more plots using the | (horizontal) and / (vertical) operators, such as #省略 ax1、ax2、ax4绘制过程 ax124 = ax1|ax2|ax4 ax124.savefig("../img/ax124.png") and #省略 ax124、ax3、ax5绘制过程 ax12435 = ax124/(ax3|ax5) ax12435.savefig("../img/ax12435.png") , demonstrating flexible grid constructions.

PythonData VisualizationseabornPlottingRggplot2patchwork
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.