matplotlib:左yaxis和右yaxis具有不同的单位

matplotlib:左yaxis和右yaxis具有不同的单位,matplotlib,axis,Matplotlib,Axis,我正在用开尔文绘制曲线。 我希望左边的Y轴以开尔文表示单位,右边的Y轴以摄氏度表示单位,并且都四舍五入到最接近的整数(因此刻度不对齐,因为TempK=TempC+273.15) 我不应该使用twinx(),因为它允许以两种不同的比例叠加曲线,这不是我的情况(只有右轴需要更改,而不是曲线)。我找到了以下解决方案: fig=plt.figure figure=fig.add_subplot(111) figure.plot(xpos, tos, color='blue') ... plot othe

我正在用开尔文绘制曲线。 我希望左边的Y轴以开尔文表示单位,右边的Y轴以摄氏度表示单位,并且都四舍五入到最接近的整数(因此刻度不对齐,因为TempK=TempC+273.15)


我不应该使用
twinx()
,因为它允许以两种不同的比例叠加曲线,这不是我的情况(只有右轴需要更改,而不是曲线)。

我找到了以下解决方案:

fig=plt.figure
figure=fig.add_subplot(111)
figure.plot(xpos, tos, color='blue')
... plot other curves if necessary
... and once all data are plot, one can create a new axis

y1, y2=figure.get_ylim()
x1, x2=figure.get_xlim()
ax2=figure.twinx()
ax2.set_ylim(y1-273.15, y2-273.15)
ax2.set_yticks( range(int(y1-273.15), int(y2-273.15), 2) )
ax2.set_ylabel('Celsius')
ax2.set_xlim(x1, x2)
figure.set_ylabel('Surface Temperature (K)')

不要忘记设置twinx轴xaxis

您需要使用
twinx()
来执行此操作,因为根据定义,您使用的是两种不同的比例。使用
setxticklabels()
定义所需的标签。谢谢。是的,实际上我在发帖后意识到了这一点,并最终找到了一个解决方案(见下文);考虑到在这种情况下,人们应该能够从两个y轴上的曲线中读取值,我将使用
仿射2d().translate()
变换,特别是当它用于交互式使用(平移/缩放)时。这使得寄生虫y轴可能偏离主y轴(或成为主y轴的刻度)。我先前的答案是使用寄生轴。是的,这个解决方案看起来很合适:下次我需要一个链接轴时,我会考虑这个问题。谢谢您的
图形
变量实际上根本不是图形,而是轴,令人困惑。
fig=plt.figure
figure=fig.add_subplot(111)
figure.plot(xpos, tos, color='blue')
... plot other curves if necessary
... and once all data are plot, one can create a new axis

y1, y2=figure.get_ylim()
x1, x2=figure.get_xlim()
ax2=figure.twinx()
ax2.set_ylim(y1-273.15, y2-273.15)
ax2.set_yticks( range(int(y1-273.15), int(y2-273.15), 2) )
ax2.set_ylabel('Celsius')
ax2.set_xlim(x1, x2)
figure.set_ylabel('Surface Temperature (K)')