Plot lower triangle in a seaborn Pairgrid

怎甘沉沦 提交于 2019-12-20 14:11:55

问题


I'm a bit struggling with seaborn Pairgrid.

Let's say I have a Pairgrid like this:

As you can see, the upper triangle plots mirror the lower triangle ones. I'd like to be able to plot only the lower triangle plots, but I found no easy way so far to do it. Can you help me?


回答1:


Probably should be an easier way, but this works

import numpy as np
import seaborn as sns
iris = sns.load_dataset("iris")
g = sns.pairplot(iris)
for i, j in zip(*np.triu_indices_from(g.axes, 1)):
    g.axes[i, j].set_visible(False)




回答2:


this is basically the same as the accepted answer, but uses the official methods from seaborn.PairGrid:

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

def hide_current_axis(*args, **kwds):
    plt.gca().set_visible(False)

g = sns.pairplot(iris)
g.map_upper(hide_current_axis)

hiding the lower half is also easy:

g.map_lower(hide_current_axis)

or hiding the diagonal:

g.map_diag(hide_current_axis)

alternatively, just use the PairGrid directly for more control:

from itertools import groupby

def stackedhist(data, stackby, **kwds):
    groups = groupby(zip(stackby, data), lambda x: x[0])
    grouped_data = [[v for _, v in items] for key, items in groups]
    plt.hist(grouped_data, stacked=True, edgecolor='none')

g = sns.PairGrid(iris, diag_sharey=False)
g.map_lower(sns.scatterplot, data=iris, hue='species', alpha=0.3, edgecolor='none')
g.map_diag(stackedhist, stackby=iris['species'])
g.map_upper(hide_current_axis)

which gives:



来源:https://stackoverflow.com/questions/34087126/plot-lower-triangle-in-a-seaborn-pairgrid

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!