patchworklib,一款极其强大的 Python 库!
来源:网络
一、问题
二、R语言
安装
# install.packages("devtools")devtools::install_github("thomasp85/patchwork")
两个图并排在一行,只需要导入patchwork, 然后相加即可
library(ggplot2)library(patchwork)p1 <- ggplot(mtcars) + geom_point(aes(mpg, disp))p2 <- ggplot(mtcars) + geom_boxplot(aes(gear, disp, group = gear))p1 + p2
两行,第一行三个图,第二行一个图
p3 <- ggplot(mtcars) + geom_smooth(aes(disp, qsec))p4 <- ggplot(mtcars) + geom_bar(aes(carb))(p1 | p2 | p3) /p4
👉点击领取:最全Python资料合集
三、Python
Patchworklib 提供了该问题的解决方案。通过使用 patchworklib,任何类型的seaborn 和plotnine 图都可以作为matplotlib 子图进行处理。安装
pip3 install patchworklib
import patchworklib as pwimport seaborn as snsfmri = 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|ax2ax12.savefig("ax12.png")
#省略 ax1、ax2、ax4绘制过程ax124 = ax1|ax2|ax4ax124.savefig("../img/ax124.png")
#省略 ax124、ax3、ax5绘制过程ax12435 = ax124/(ax3|ax5)ax12435.savefig("../img/ax12435.png")
热门推荐