问题
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