使用python设置xticks标签频率

时间:2016-09-29 11:14:41

标签: python matplotlib

我想减少x刻度的频率。 x值是日期:

date=['20120101','20120101','20120101',...'20121231']

我一年中的每一天都有24个时间步。我想每24个步骤(即每天)标记xticks。

以下是我使用的代码:

date=[]
val=[]
for lig in file(liste.txt):
   ligne=lig.split('')
   date.append(ligne[0])
   val.append(ligne[1])

plt.plot(date,val)
plt.setp(plt.gca().xaxis.get_majorticklabels(),rotation=90)
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%y%m%d'))
plt.gca().xaxis.set_major_locator(mdates.DayLocator(interval=4))
plt.show()

这是我的情节,没有xticks标签!

enter image description here

1 个答案:

答案 0 :(得分:1)

由于没有完整的和工作示例,我们必须猜测:您的date是否被正确识别为datetime对象?如果不是,日期格式中的set_major_locator将导致没有x-ticks。

您可以使用date = datetime.datetime.strptime(date, '%Y%m%d')等内容来实现此目的。