出于某种原因,在我向绘图添加第二个 y 轴之后
fig.plt.figure()
ax = plt.Axes(fig)
fig.add_axes(ax)
ax2 = ax.twinx()
fig.add_axes(ax2)
xticklabels 不再旋转!?
fig.autofmt_xdate(rotation = num)
有人知道为什么会这样吗?
我可以注释掉最后两行:
#ax2 = ax.twinx()
#fig.add_axes(ax2)
它会旋转 xticklabels。
请您参考如下方法:
将 fig.autofmt_xdate(rotation = num) 放在定义 ax 的语句之前调用ax.twinx() 和:
import matplotlib.pyplot as plt
import matplotlib.dates as md
import datetime as dt
import numpy as np
np.random.seed(0)
t=md.drange(dt.datetime(2009,10,1),
dt.datetime(2010,1,15),
dt.timedelta(days=1))
n=len(t)
x1 = np.cumsum(np.random.random(n) - 0.5) * 40000
x2 = np.cumsum(np.random.random(n) - 0.5) * 0.002
fig = plt.figure()
# fig.autofmt_xdate(rotation=25) # does not work
ax1 = fig.add_subplot(1,1,1)
fig.autofmt_xdate(rotation=25) # works
ax2 = ax1.twinx()
# fig.autofmt_xdate(rotation=25) # does not work
ax1.plot_date(t, x1, 'r-')
ax2.plot_date(t, x2, 'g-')
plt.show()
产量
