gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 14:46:22 28 4
gpt4 key购买 nike

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

fig=plt.figure
figure=fig.add_subplot(111)
figure.plot(xpos, tos, color='blue')

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

最佳答案

enter image description here我找到了以下解决方案:

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 Axis xaxis!

关于matplotlib:左 yaxis 和右 yaxis 具有不同的单位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17297608/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com