【发布时间】:2018-05-27 20:45:03
【问题描述】:
我试图弄清楚如何更改图表 x 轴上日期的旋转。见下图。我有如何做到这一点的例子,但它们不匹配,因为我有双 y 轴。你能帮忙改变日期的轮换吗?
这是我的代码:
fig, ax1 = plt.subplots()
fig = plt.figure(figsize=(8,6))
t = df['date']
s1 = df['msft']
ax1.plot(t, s1, 'b-')
ax1.set_xlabel('Dates')
ax1.legend(loc=0)
ax1.grid()
# Make the y-axis label, ticks and tick labels match the line color.
ax1.set_ylabel('Price', color='b')
ax1.tick_params('y', colors='b')
ax2 = ax1.twinx()
s2 = df['amzn']
ax2.plot(t, s2, 'r-')
ax2.set_ylabel('amzn', color='r')
ax2.tick_params('date', colors='r')
ax2.legend(loc=0)
fig.tight_layout()
plt.show()
【问题讨论】:
-
可能是
plt.xticks(rotation=90)? -
或者,
plt.gcf().autofmt_xdate() -
那些都不行,你有其他想法吗?
-
ax1.set_xticklabels(t, rotation=45),我添加了这一行并将日期设置为 45 度角。
标签: python matplotlib axis-labels