如何在添加另一个yaxis的同时旋转xlabels?

Cas*_*asa 1 python matplotlib

出于某种原因,在我向我的情节添加第二个y轴之后

fig.plt.figure()
ax = plt.Axes(fig)
fig.add_axes(ax)
ax2 = ax.twinx()
fig.add_axes(ax2)
Run Code Online (Sandbox Code Playgroud)

xticklabels不再旋转!?

fig.autofmt_xdate(rotation = num)
Run Code Online (Sandbox Code Playgroud)

有谁知道为什么会这样?

我可以评论最后两行:

#ax2 = ax.twinx()
#fig.add_axes(ax2)
Run Code Online (Sandbox Code Playgroud)

它将旋转xticklabels.

unu*_*tbu 6

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()
Run Code Online (Sandbox Code Playgroud)

产量

在此输入图像描述