【发布时间】:2014-08-13 06:32:24
【问题描述】:
我的matplotlib pyplot 有太多xticks - 它目前在 15 年期间每年和每月都显示,例如“2001-01”,但我只希望 x 轴显示年份(例如 2001 年)。
输出将是一个折线图,其中 x 轴显示日期,y 轴显示销售和租金价格。
# Defining the variables
ts1 = prices['Month'] # eg. "2001-01" and so on
ts2 = prices['Sale']
ts3 = prices['Rent']
# Reading '2001-01' as year and month
ts1 = [dt.datetime.strptime(d,'%Y-%m').date() for d in ts1]
plt.figure(figsize=(13, 9))
# Below is where it goes wrong. I don't know how to set xticks to show each year.
plt.xticks(ts1, rotation='vertical')
plt.xlabel('Year')
plt.ylabel('Price')
plt.plot(ts1, ts2, 'r-', ts1, ts3, 'b.-')
plt.gcf().autofmt_xdate()
plt.show()
【问题讨论】:
标签: python matplotlib