我的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()
np.arange(0, len(dates), 12)
。 - Pythontologyplt.xticks(ts1[0::12])
。(在此处查看双冒号选择器的行为示例。) - Pythontology