matplotlib 第二轴标签不显示

matplotlib 2nd axis label not showing

使用 matplotlib 如何显示第二个 y 轴标签?这是我试过的:

import matplotlib.pyplot as plt
x = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.65]
y = [0, 0.15, 0.3, 0.35, 0.4, 0.55, 0.57, 0.58]
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx().twiny()
ax2.set_xlabel('2nd x-axis label')
ax2.set_ylabel('2nd y-axis label')
ax1.set_xlim([0,1])
ax1.plot(x, y)
plt.show()

twinxtwiny 实际上创建了单独的坐标区对象。由于您设置了 ax2 = ax1.twinx().twiny(),您只是 "saving" twiny 调用的结果,而不是 twinx 调用的结果。你需要在两个轴上分别设置x和y标签,这意味着你需要保存twinx个轴以供以后访问:

import matplotlib.pyplot as plt
x = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.65]
y = [0, 0.15, 0.3, 0.35, 0.4, 0.55, 0.57, 0.58]
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
ax3 = ax2.twiny()
ax3.set_xlabel('2nd x-axis label')
ax2.set_ylabel('2nd y-axis label')
ax1.set_xlim([0,1])
ax1.plot(x, y)