如何将这个直方图中的蓝色条带到前面?

2024-06-09 06:32:05 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有代码可以把这个直方图中的蓝色条带到前面?如果我去掉alpha,我就看不到蓝色的条了。我在用Python3和海伯恩来做这个阴谋

先谢谢你

g = sns.FacetGrid(df, hue='credit.policy', palette='coolwarm', height =5, aspect=2)
g = g.map(plt.hist, 'fico', alpha=0.8, bins=30).add_legend()

enter image description here


Tags: 代码alphadfpolicy直方图huepython3蓝色
2条回答

正如@trenton mckinney所建议的,您可以使用hue_order来控制类别的绘制顺序。比较:

df = pd.concat([pd.DataFrame({'data':np.random.normal(loc=0, scale=0.25, size=(1000,)), 'category':0}),pd.DataFrame({'data':np.random.normal(loc=1, size=(1000,)), 'category':1})])



g = sns.FacetGrid(df, hue='category', palette='coolwarm', height=5, aspect=2, hue_order=[0,1])
g = g.map(plt.hist, 'data', alpha=0.8, bins=30).add_legend()

enter image description here

g = sns.FacetGrid(df, hue='category', palette='coolwarm', height=5, aspect=2, hue_order=[1,0])
g = g.map(plt.hist, 'data', alpha=0.8, bins=30).add_legend()

enter image description here

你可以用下面的代码实现你想要的

import matplotlib.pyplot as plt
plt.hist(df[df.Private == 'No']['F.Undergrad'], alpha = 0.6,color='c')
plt.hist(df[df.Private == 'Yes']['F.Undergrad'],alpha = 0.9,color='g')
plt.show()

输出上述代码

enter image description here

plt.hist(df[df.Private == 'Yes']['F.Undergrad'],alpha = 0.6,color='g')
plt.hist(df[df.Private == 'No']['F.Undergrad'], alpha = 0.9,color='c')
plt.show()

输出上述代码

enter image description here

相关问题 更多 >