【问题标题】:How to change the diagonal rotation of a x-axis label on chart?如何更改图表上 x 轴标签的对角线旋转?
【发布时间】: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


【解决方案1】:

我添加了ax1.set_xticklabels(t, rotation=45),这条线使日期呈 45 度角。 ——

【讨论】:

    猜你喜欢
    • 2015-10-06
    • 1970-01-01
    • 2020-08-23
    • 2020-10-21
    • 2013-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多