matplotlib减少x轴坐标刻度,将数值刻度自定义为字符串格式的日期
原始图:
plt.plot(data[0:2016],label='flow')
plt.plot(speed[0:2016],label='speed')
plt.plot(occupy[0:2016],label='occupy')
plt.legend(loc='upper right')
plt.show()
x轴坐标为0-2000的数值刻度
现要将x轴坐标设置为2018/1/1到2018/1/8的间隔为7的字符串形式的日期坐标:
先用plt.xticks()方法减小间隔:
plt.xticks([0,288,576,864,1152,1440,1728,2016],rotation=45)
set_xticklabels重新设置刻度标签:
set_xticklabels(dates)
from datetime import datetime
import matplotlib.dates as mdates
from matplotlib.pylab import style
from PyQt5.QtGui import *
style.use('ggplot')
f, ax = plt.subplots()
plt.plot(data[0:2016],label='flow')
plt.plot(speed[0:2016],label='speed')
plt.plot(occupy[0:2016],label='occupy')
plt.xlabel('date',fontsize=20)
plt.ylabel('value',fontsize=20)
plt.xticks([0,288,576,864,1152,1440,1728,2016],rotation=45)
dates = ['2018/01/01', '2018/01/02', '2018/01/03','2018/01/04','2018/01/05','2018/01/06','2018/01/07','2018/01/08']
ax.set_xticklabels(dates)
plt.set_title()
plt.legend(loc='upper right')
plt.show()
效果如下: