如何在 Subplot 中绘制多个 Seaborn Jointplot
- 2025-02-11 09:50:00
- admin 原创
- 95
问题描述:
我在将 Seaborn 放置Jointplot
在多列内时遇到了问题subplot
。
import pandas as pd
import seaborn as sns
df = pd.DataFrame({'C1': {'a': 1,'b': 15,'c': 9,'d': 7,'e': 2,'f': 2,'g': 6,'h': 5,'k': 5,'l': 8},
'C2': {'a': 6,'b': 18,'c': 13,'d': 8,'e': 6,'f': 6,'g': 8,'h': 9,'k': 13,'l': 15}})
fig = plt.figure();
ax1 = fig.add_subplot(121);
ax2 = fig.add_subplot(122);
sns.jointplot("C1", "C2", data=df, kind='reg', ax=ax1)
sns.jointplot("C1", "C2", data=df, kind='kde', ax=ax2)
请注意,只有一部分jointplot
被放置在子图内,其余部分则留在另外两个图框内。我想要的是将两者都distributions
插入到内subplots
。
有人可以帮忙吗?
解决方案 1:
在 matplotlib 中移动轴并不像以前的版本那么容易。下面是使用当前版本的 matplotlib 的操作。
正如在多个地方(这个问题,还有这个问题)指出的那样,几个 seaborn 命令会自动创建自己的图形。这是硬编码到 seaborn 代码中的,因此目前无法在现有图形中生成此类图表。这些是PairGrid
、、、和FacetGrid
。JointGrid
`pairplotjointplot
lmplot`
有一个可用的 seaborn fork,它允许为相应的类提供子图网格,以便在预先存在的图形中创建图。要使用此功能,您需要将axisgrid.py
fork 中的复制到 seaborn 文件夹。请注意,目前仅限于与 matplotlib 2.1(也可能是 2.0)一起使用。
另一种方法是创建一个 seaborn 图形并将轴复制到另一个图形。此答案中显示了此原理,并且可以扩展到 Searborn 图。实现比我最初预期的要复杂一些。以下是一个SeabornFig2Grid
可以使用 seaborn 网格实例(上述任何命令的返回值)、matplotlib 图形和 (subplot_spec
即网格的位置)来调用的类gridspec
。
注意:这是一个概念证明,它可能适用于大多数简单情况,但我不建议在生产代码中使用它。
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import seaborn as sns
import numpy as np
class SeabornFig2Grid():
def __init__(self, seaborngrid, fig, subplot_spec):
self.fig = fig
self.sg = seaborngrid
self.subplot = subplot_spec
if isinstance(self.sg, sns.axisgrid.FacetGrid) or \n isinstance(self.sg, sns.axisgrid.PairGrid):
self._movegrid()
elif isinstance(self.sg, sns.axisgrid.JointGrid):
self._movejointgrid()
self._finalize()
def _movegrid(self):
""" Move PairGrid or Facetgrid """
self._resize()
n = self.sg.axes.shape[0]
m = self.sg.axes.shape[1]
self.subgrid = gridspec.GridSpecFromSubplotSpec(n,m, subplot_spec=self.subplot)
for i in range(n):
for j in range(m):
self._moveaxes(self.sg.axes[i,j], self.subgrid[i,j])
def _movejointgrid(self):
""" Move Jointgrid """
h= self.sg.ax_joint.get_position().height
h2= self.sg.ax_marg_x.get_position().height
r = int(np.round(h/h2))
self._resize()
self.subgrid = gridspec.GridSpecFromSubplotSpec(r+1,r+1, subplot_spec=self.subplot)
self._moveaxes(self.sg.ax_joint, self.subgrid[1:, :-1])
self._moveaxes(self.sg.ax_marg_x, self.subgrid[0, :-1])
self._moveaxes(self.sg.ax_marg_y, self.subgrid[1:, -1])
def _moveaxes(self, ax, gs):
#https://stackoverflow.com/a/46906599/4124317
ax.remove()
ax.figure=self.fig
self.fig.axes.append(ax)
self.fig.add_axes(ax)
ax._subplotspec = gs
ax.set_position(gs.get_position(self.fig))
ax.set_subplotspec(gs)
def _finalize(self):
plt.close(self.sg.fig)
self.fig.canvas.mpl_connect("resize_event", self._resize)
self.fig.canvas.draw()
def _resize(self, evt=None):
self.sg.fig.set_size_inches(self.fig.get_size_inches())
此类的用法如下:
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import seaborn as sns; sns.set()
import SeabornFig2Grid as sfg
iris = sns.load_dataset("iris")
tips = sns.load_dataset("tips")
# An lmplot
g0 = sns.lmplot(x="total_bill", y="tip", hue="smoker", data=tips,
palette=dict(Yes="g", No="m"))
# A PairGrid
g1 = sns.PairGrid(iris, hue="species")
g1.map(plt.scatter, s=5)
# A FacetGrid
g2 = sns.FacetGrid(tips, col="time", hue="smoker")
g2.map(plt.scatter, "total_bill", "tip", edgecolor="w")
# A JointGrid
g3 = sns.jointplot("sepal_width", "petal_length", data=iris,
kind="kde", space=0, color="g")
fig = plt.figure(figsize=(13,8))
gs = gridspec.GridSpec(2, 2)
mg0 = sfg.SeabornFig2Grid(g0, fig, gs[0])
mg1 = sfg.SeabornFig2Grid(g1, fig, gs[1])
mg2 = sfg.SeabornFig2Grid(g2, fig, gs[3])
mg3 = sfg.SeabornFig2Grid(g3, fig, gs[2])
gs.tight_layout(fig)
#gs.update(top=0.7)
plt.show()
请注意,复制轴可能会存在一些缺点,并且上述内容尚未经过彻底测试。
解决方案 2:
如果不进行黑客攻击,这很难做到。 jointplot
调用方法,该方法每次被调用时JointGrid
都会创建一个新对象。figure
因此,破解方法是制作两个联合图(JG1
JG2
),然后创建一个新图形,然后将轴对象从迁移JG1
JG2
到新创建的图形。
最后,我们调整刚刚创建的新图形中子图的大小和位置。
JG1 = sns.jointplot("C1", "C2", data=df, kind='reg')
JG2 = sns.jointplot("C1", "C2", data=df, kind='kde')
#subplots migration
f = plt.figure()
for J in [JG1, JG2]:
for A in J.fig.axes:
f._axstack.add(f._make_key(A), A)
#subplots size adjustment
f.axes[0].set_position([0.05, 0.05, 0.4, 0.4])
f.axes[1].set_position([0.05, 0.45, 0.4, 0.05])
f.axes[2].set_position([0.45, 0.05, 0.05, 0.4])
f.axes[3].set_position([0.55, 0.05, 0.4, 0.4])
f.axes[4].set_position([0.55, 0.45, 0.4, 0.05])
f.axes[5].set_position([0.95, 0.05, 0.05, 0.4])
这是一种黑客行为,因为我们现在正在使用_axstack
私有_add_key
方法,它们在未来的版本中可能会也可能不会与现在保持不变matplotlib
。
解决方案 3:
最近,我正在开发patchworklib/
,这是一个受 patchwork 启发的 matplotlib 子图管理器。它允许您仅使用和运算符快速排列多个网格 seaborn 图|
。
以下是示例代码,您也可以在 Google colab 上运行它:
import seaborn as sns
import patchworklib as pw
sns.set_theme()
pw.overwrite_axisgrid()
iris = sns.load_dataset("iris")
tips = sns.load_dataset("tips")
# An lmplot
g0 = sns.lmplot(x="total_bill", y="tip", hue="smoker", data=tips,
palette=dict(Yes="g", No="m"))
g0 = pw.load_seaborngrid(g0, label="g0")
# A Pairplot
g1 = sns.pairplot(iris, hue="species")
g1 = pw.load_seaborngrid(g1, label="g1")
# A relplot
g2 = sns.relplot(data=tips, x="total_bill", y="tip", col="time", hue="time",
size="size", style="sex", palette=["b", "r"], sizes=(10, 100))
g2 = pw.load_seaborngrid(g2, label="g2")
# A JointGrid
g3 = sns.jointplot("sepal_width", "petal_length", data=iris,
kind="kde", space=0, color="g")
g3 = pw.load_seaborngrid(g3, label="g3")
(((g0|g1)["g0"]/g3)["g3"]|g2).savefig("seaborn_subplots.png")
解决方案 4:
如果尽管@ImportanceOfBeingErnest 的解决方案很优雅,您仍然会遇到麻烦,您仍然可以将 seaborn 图作为图像保存到内存中,并使用它们来构建自定义图形。如果您寻求更高的分辨率,请使用除“.png”之外的其他格式。
下面是使用这种不太好(但有效)的方法的例子:
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import seaborn as sns
# data
iris = sns.load_dataset("iris")
tips = sns.load_dataset("tips")
############### 1. CREATE PLOTS
# An lmplot
g0 = sns.lmplot(x="total_bill", y="tip", hue="smoker", data=tips,
palette=dict(Yes="g", No="m"))
# A PairGrid
g1 = sns.PairGrid(iris, hue="species")
g1.map(plt.scatter, s=5)
# A FacetGrid
g2 = sns.FacetGrid(tips, col="time", hue="smoker")
g2.map(plt.scatter, "total_bill", "tip", edgecolor="w")
# A JointGrid
g3 = sns.jointplot("sepal_width", "petal_length", data=iris,
kind="kde", space=0, color="g")
############### 2. SAVE PLOTS IN MEMORY TEMPORALLY
g0.savefig('g0.png')
plt.close(g0.fig)
g1.savefig('g1.png')
plt.close(g1.fig)
g2.savefig('g2.png')
plt.close(g2.fig)
g3.savefig('g3.png')
plt.close(g3.fig)
############### 3. CREATE YOUR SUBPLOTS FROM TEMPORAL IMAGES
f, axarr = plt.subplots(2, 2, figsize=(25, 16))
axarr[0,0].imshow(mpimg.imread('g0.png'))
axarr[0,1].imshow(mpimg.imread('g1.png'))
axarr[1,0].imshow(mpimg.imread('g3.png'))
axarr[1,1].imshow(mpimg.imread('g2.png'))
# turn off x and y axis
[ax.set_axis_off() for ax in axarr.ravel()]
plt.tight_layout()
plt.show()
解决方案 5:
关注@https://stackoverflow.com/users/4124317/importanceofbeingernest answer。我已向其类添加了附加函数_moveclustergrid来处理 seaborn 的 clusterheatmaps(ClusterGrid类)。
class SeabornFig2Grid():
"""Class from https://stackoverflow.com/questions/47535866/how-to-iteratively-populate-matplotlib-gridspec-with-a-multipart-seaborn-plot/47624348#47624348"""
def __init__(self, seaborngrid, fig, subplot_spec):
self.fig = fig
self.sg = seaborngrid
self.subplot = subplot_spec
if isinstance(self.sg, sns.axisgrid.FacetGrid) or \n isinstance(self.sg, sns.axisgrid.PairGrid):
self._movegrid()
elif isinstance(self.sg, sns.axisgrid.JointGrid):
self._movejointgrid()
elif isinstance(self.sg, sns.matrix.ClusterGrid):
self._moveclustergrid()
else:
print("what am i?")
self._finalize()
def _movegrid(self):
""" Move PairGrid or Facetgrid """
self._resize()
n = self.sg.axes.shape[0]
m = self.sg.axes.shape[1]
self.subgrid = gridspec.GridSpecFromSubplotSpec(n,m, subplot_spec=self.subplot)
for i in range(n):
for j in range(m):
self._moveaxes(self.sg.axes[i,j], self.subgrid[i,j])
def _movejointgrid(self):
""" Move Jointgrid """
h= self.sg.ax_joint.get_position().height
h2= self.sg.ax_marg_x.get_position().height
r = int(np.round(h/h2))
self._resize()
self.subgrid = gridspec.GridSpecFromSubplotSpec(r+1,r+1, subplot_spec=self.subplot)
self._moveaxes(self.sg.ax_joint, self.subgrid[1:, :-1])
self._moveaxes(self.sg.ax_marg_x, self.subgrid[0, :-1])
self._moveaxes(self.sg.ax_marg_y, self.subgrid[1:, -1])
def _moveclustergrid(self):
"""Move Cluster grid"""
r = len(self.sg.figure.axes)
self.subgrid = gridspec.GridSpecFromSubplotSpec(r, r + 10, subplot_spec=self.subplot)
subplots_axes = self.sg.figure.axes
self._resize()
self._moveaxes(subplots_axes[0], self.subgrid[1:, 0:3]) #left cladogram
self._moveaxes(subplots_axes[1], self.subgrid[0, 4:-2]) #top cladogram
self._moveaxes(subplots_axes[2], self.subgrid[1:, 3]) #labels bar
self._moveaxes(subplots_axes[3], self.subgrid[1:, 4:-3]) #heatmap
self._moveaxes(subplots_axes[4], self.subgrid[1:, -1]) #colorbar
def _moveaxes(self, ax, gs):
#https://stackoverflow.com/a/46906599/4124317
ax.remove()
ax.figure=self.fig
self.fig.axes.append(ax)
self.fig.add_axes(ax)
ax._subplotspec = gs
ax.set_position(gs.get_position(self.fig))
ax.set_subplotspec(gs)
def _finalize(self):
plt.close(self.sg.fig)
self.fig.canvas.mpl_connect("resize_event", self._resize)
self.fig.canvas.draw()
def _resize(self, evt=None):
self.sg.fig.set_size_inches(self.fig.get_size_inches())
它不是 100%“漂亮情节”的证明,但它提供了一个起点。只需通过修改它们在self.subgrid[rows,columns]中使用的行数和列数来更改子图的位置
请参阅链接中的输出(我没有足够的声誉来嵌入图像):clustermaps_side_by_side
扫码咨询,免费领取项目管理大礼包!