python - 如何在子图中绘制多个 Seaborn 联合图

标签 python python-3.x pandas matplotlib seaborn

我在将 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 中。

有人可以帮忙吗?

最佳答案

在 matplotlib 中移动轴不像以前那样容易。以下是使用当前版本的 matplotlib。

正如在几个地方( this question ,还有 this issue )所指出的,一些 seaborn 命令会自动创建自己的图形。这是硬编码到 seaborn 代码中的,因此目前无法在现有图形中生成此类图。它们是 PairGridFacetGridJointGridpairplotjointplot lmplot

有一个seaborn fork available这将允许为各个类提供子图网格,以便在预先存在的图形中创建该图。要使用它,您需要将 axisgrid.py 从 fork 复制到 seaborn 文件夹。请注意,这目前仅限用于 matplotlib 2.1(也可能是 2.0)。

另一种方法是创建一个 seaborn 图形并将轴复制到另一个图形。其原理见this answer并且可以扩展到 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 \
            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()

enter image description here

请注意,复制轴可能有几个缺点,并且上述内容尚未(尚未)彻底测试。

关于python - 如何在子图中绘制多个 Seaborn 联合图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35042255/

相关文章:

python - django 1.7 & 删除 postgres 数据库并重新创建它

python - 如何防止 pandas 数据框中的索引显示在 Excel 中?

python - 按周划分 Pandas Dataframe

python - 如何将列表或显示剪切为多个部分

python - 如果行不存在,使用 Python 将数据更新到 mysql

python - 如何将一些行转换为相同的索引 - Python

python - 重命名 Pandas 数据框中的选定列

python - 整数除法 : is a//b == int(a/b) true for all integers a, b?

python - 了解装饰器的真正工作原理

pandas - 如何重命名pandas中的一批列?