更改 KDE 的颜色和有边订单 matplotlib.plot
Change colour and leged order for a KDE matplotlib.plot
df.groupby("class").q1.plot(kind='kde')
plt.title('Age')
plt.xlabel('Score obtained in age')
plt.ylabel('Density')
plt.legend()
plt.savefig(f'Analysis/q1-kde.png')
plt.close()
label_order=["Very Low", "Low", 'Average', 'High', 'Very High']
并得到类似的东西:
但是,图例标签是按字母顺序排序的。我希望图例和 kde 行的顺序与 label_order
中定义的顺序相同
将 categorical
dtype 与命令一起使用:
label_order=["Very Low", "Low", 'Average', 'High', 'Very High']
df['class'] = pd.Categorical(df['class'], categories=label_order, ordered=True)
df.groupby("class").q1.plot(kind='kde')
plt.title('Age')
plt.xlabel('Score obtained in age')
plt.ylabel('Density')
plt.legend()
输出:
df.groupby("class").q1.plot(kind='kde')
plt.title('Age')
plt.xlabel('Score obtained in age')
plt.ylabel('Density')
plt.legend()
plt.savefig(f'Analysis/q1-kde.png')
plt.close()
label_order=["Very Low", "Low", 'Average', 'High', 'Very High']
并得到类似的东西:
但是,图例标签是按字母顺序排序的。我希望图例和 kde 行的顺序与 label_order
将 categorical
dtype 与命令一起使用:
label_order=["Very Low", "Low", 'Average', 'High', 'Very High']
df['class'] = pd.Categorical(df['class'], categories=label_order, ordered=True)
df.groupby("class").q1.plot(kind='kde')
plt.title('Age')
plt.xlabel('Score obtained in age')
plt.ylabel('Density')
plt.legend()
输出: