pandas - 更改 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 几乎相同,我试图避免这种情况,以便能够显示分布的真实形状。
解决方案
我想我使用上一个答案和这个视频的灵感找到了解决方案:
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)]
:
以为我会把它留在这里 - 虽然小众,但将来可能会对某人有所帮助!
推荐阅读
- python - 为考试练习python任务?
- ruby-on-rails - 在 Searchkick rails 5 中过滤掉其他用户的笔记
- java - 无法弄清楚如何使用 Spring 2.0+ 在 Jedis 中设置 SSL 连接
- midi - 在线设置 MIDI 速度
- sql - 带有加法和更大/更少运算符的 CASE 语法
- ansible - 如何在lineinfile ansible中使用转义字符
- python-3.x - conflicting cases in python lists
- r - 如何在分组数据时为 stat_ellipse 使用固定颜色?
- ios - 当无法找到故事板或尚未创建故事板时,如何防止 SWIFT 4.2 运行时错误?
- c - 为 C 安装 libavl