Seaborn两个角对线图 [英] seaborn two corner pairplot

查看:58
本文介绍了Seaborn两个角对线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用两个角对线图创建单个线对图.使用

I'd like to create a single pairplot using two corner pairplot. Using

import seaborn as sns; sns.set(style="ticks", color_codes=True)
iris = sns.load_dataset("iris")
g = sns.pairplot(iris, hue="species", corner=True)

我获得了网格的下三角形.我想做的是使用不同的色相值在网格的上部(非对角线)放置另一个对图.

I obtain a lower triangle of the grid. What I'd like to do is to put another pairplot on the upper (off-diagonal) part of the grid using a different value for hue.

import seaborn as sns; sns.set(style="ticks", color_codes=True)
iris = sns.load_dataset("iris")
iris['species'] = iris['species'].map({'setosa': 0, 
                                   'versicolor': 1, 
                                   'virginica': 2})

sns.pairplot(iris, hue="species", corner=True)
sns.pairplot(iris, hue="petal_length", corner=True)

有没有一种方法可以绘制上三角形?还是加入两个不同的对图?

Is there a way to plot on the upper triangle? Or join two different pairplot?

预先感谢

推荐答案

无法在上三角形上绘图.但是,您可以做的是绘制两个图,其中至少一个带有 corner=False,然后将角图的下三角形和对角轴添加到完整图.但是,这仅在两个绘图的pairplot参数相同时才有意义,否则(如您的示例所示),轴标签和图例仅对一个三角形有效(除非您手动添加第二个图例以及右轴和上轴到上三角形 suplots,但在这种情况下,从一开始就可以更容易地滚动).

There's no way to plot on the upper triangle. What you could do, however, is to make two plots, minimum one of them with corner=False, and then add the lower triangle and diagonal axes from the corner plot to the full plot. This only makes sense, however, if the pairplot parameters for both plot are identical, otherwise (as in your example) the axes labels and the legend will be valid for one triangle only (unless you manually add a second legend and right and top axes to the upper triangle suplots but in this case it'll probably easier to roll your own from the very beginning).

示例(iris 奇数行的下三角和对角线,偶数行的上三角):

Example (lower triangle and diagonal for odd rows of iris, upper triangle for even rows):

import matplotlib.pyplot as plt
import seaborn as sns; sns.set(style="ticks", color_codes=True)
iris = sns.load_dataset("iris")

pg1 = sns.pairplot(iris[1::2], hue="species", corner=True)
pg2 = sns.pairplot(iris[::2], hue="species", corner=False, diag_kind=None))

# remove lower triangle and diagonal from figure 2
for ax in pg2.fig.get_axes():
    if ax.get_geometry()[2] in [1,5,6,9,10,11,13,14,15,16]:
        ax.remove()

# add all axes from figure 1 (lower triangle and diagonal) to figure 2
for ax in pg1.fig.get_axes():
    ax.figure = pg2.fig # in the next step we can only add axes from the same figure
    pg2.fig.add_axes(ax)

# close figure 1 which is not needed anymore    
plt.close(pg1.fig)

这篇关于Seaborn两个角对线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆