首页 > 解决方案 > 更改 seaborn 直方图(或 plt)中数据选择的条形颜色

问题描述

假设我有一个数据框,例如:

X2 = np.random.normal(10, 3, 200)
X3 = np.random.normal(34, 2, 200)

a = pd.DataFrame({"X3": X3, "X2":X2})

我正在执行以下绘图程序:

f, axes = plt.subplots(2, 2,  gridspec_kw={"height_ratios":(.10, .30)}, figsize = (13, 4))
for i, c in enumerate(a.columns):
    sns.boxplot(a[c], ax=axes[0,i])
    sns.distplot(a[c], ax = axes[1,i])
    axes[1, i].set(yticklabels=[])
    axes[1, i].set(xlabel='')
    axes[1, i].set(ylabel='')

plt.tight_layout()
plt.show()

这产生:

在此处输入图像描述

现在我希望能够对数据框 a 执行数据选择。让我们这样说:

b = a[(a['X2'] <4)]

并在发布的直方图中突出显示 b 中的选择。例如,如果 b 的第一行是 X3 的 [32:0] 和 X2 的 [0:5],则所需的输出将是:

在此处输入图像描述

可以用上面的for循环和sns来做到这一点吗?非常感谢!

编辑:如果更简单的话,我也对 matplotlib 解决方案感到满意。

编辑2:

如果它有帮助,它会类似于执行以下操作:

b = a[(a['X3'] >38)]

f, axes = plt.subplots(2, 2,  gridspec_kw={"height_ratios":(.10, .30)}, figsize = (13, 4))

for i, c in enumerate(a.columns):
   sns.boxplot(a[c], ax=axes[0,i])
   sns.distplot(a[c], ax = axes[1,i])
   sns.distplot(b[c], ax = axes[1,i])

   axes[1, i].set(yticklabels=[])
   axes[1, i].set(xlabel='')
   axes[1, i].set(ylabel='')

plt.tight_layout()
plt.show()

产生以下结果:

在此处输入图像描述

但是,我希望能够只用不同的颜色为第一个图中的那些条上色! 我还考虑将 ylim 设置为仅蓝色图的大小,这样橙色就不会扭曲蓝色分布的形状,但这仍然不可行,因为实际上我有大约 10 个直方图要显示,并且设置 ylim 与 sharey=True 几乎相同,我试图避免这种情况,以便能够显示分布的真实形状。

标签: pandasmatplotlibseaborndistribution

解决方案


我想我使用上一个答案和这个视频的灵感找到了解决方案:

import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

np.random.seed(2021)
X2 = np.random.normal(10, 3, 200)
X3 = np.random.normal(34, 2, 200)

a = pd.DataFrame({"X3": X3, "X2":X2})
b = a[(a['X3'] < 30)]


hist_idx=[]

for i, c in enumerate(a.columns):
    bin_ = np.histogram(a[c], bins=20)[1]
    hist = np.where(np.logical_and(bin_<=max(b[c]), bin_>min(b[c])))
    hist_idx.append(hist)
    

f, axes = plt.subplots(2, 2,  gridspec_kw={"height_ratios":(.10, .30)}, figsize = (13, 4))

for i, c in enumerate(a.columns):
    sns.boxplot(a[c], ax=axes[0,i])
    axes[1, i].hist(a[c], bins = 20)
    axes[1, i].set(yticklabels=[])
    axes[1, i].set(xlabel='')
    axes[1, i].set(ylabel='')
    
for it, index in enumerate(hist_idx):
    lenght = len(index[0])
    for r in range(lenght):
        try:
            axes[1, it].patches[index[0][r]-1].set_fc("red")
        except:
            pass 


plt.tight_layout()
plt.show()

产生以下结果b = a[(a['X3'] < 30)]

在此处输入图像描述

或为b = a[(a['X3'] > 36)]在此处输入图像描述

以为我会把它留在这里 - 虽然小众,但将来可能会对某人有所帮助!


推荐阅读