将自定义文本绘制为xlabels

时间:2016-06-13 11:37:39

标签: python numpy matplotlib

我正在尝试用文本重命名我的x标签并编写以下代码:

dow=pd.to_datetime(df1['Dates']).dt.dayofweek
Weekdays= ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
df2 = df1.groupby([dow,'Category'])['Category'].count().unstack('Category').fillna(0)
df2.plot(kind='bar', stacked=True,figsize=(12,6))
plt.xlabel("Day of week",fontsize=15)
plt.ylabel("Total Incidents",fontsize=15)
plt.xticks(dow,Weekdays)
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))

然而,所有文字都被添加到彼此之上,如下所示

enter image description here

不知道该解决什么问题?

2 个答案:

答案 0 :(得分:1)

尝试:

plt.xticks(range(len(Weekdays)),Weekdays)

答案 1 :(得分:1)

尝试:

ax = plt.gca()
ax.set_xticks([day for day in Weekdays])

或:

plt.xticks([day for day in Weekdays])