How to bring the blue bars in this histogram plot to the front?

佐手、 提交于 2019-12-12 01:28:33

问题


Is there a code that can bring the blue bars in this histogram plot to the front? If I remove alpha I can't see the blue bars at all. I'm using Python3 and Seaborn for this plot

Thank you in advance

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()


回答1:


You can achieve what you want with following code,

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()

Output of above code,

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()

Output of above code,




回答2:


As suggested by @trenton-mckinney, you can use hue_order to control the order in which your categories are plotted. Compare:

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()

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()



来源:https://stackoverflow.com/questions/58984908/how-to-bring-the-blue-bars-in-this-histogram-plot-to-the-front

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