matplotlib第二轴标签未显示

时间:2015-02-13 19:18:49

标签: python matplotlib axis-labels

使用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()

enter image description here

1 个答案:

答案 0 :(得分:2)

twinxtwiny实际上创建了单独的轴对象。由于您设置了ax2 = ax1.twinx().twiny(),因此您只能"保存" 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)