2013-06-25 25 views
4

我在Kelvin繪製曲線。 我想有左Y軸顯示在開爾文單位和右y軸向他們展示攝氏度,都四捨五入到最接近的整數(所以蜱沒有對齊,如TempK = TempC + 273.15)matplotlib:左yaxis和右yaxis有不同的單位

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

我不應該使用twinx(),因爲它允許疊加具有兩種不同比例的曲線,這不是我的情況(只有右軸必須更改,而不是曲線)。

+0

您需要使用'twinx()'來做到這一點,因爲根據定義,您使用了兩種不同的比例尺。使用'set_xticklabels()'來定義你想要的標籤。 – Greg

+0

謝謝。是的,其實我發現後發現,並最終找到了解決方案(見下文)。 –

+0

這篇文章到郵件列表可能會有所幫助; http://sourceforge.net/mailarchive/forum.php?thread_name=012701ce5892%24683e5cb0%2438bb1610%24%40earthlink.net&forum_name=matplotlib-devel – tacaswell

回答

5

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軸x軸!

+2

你的'figure'變量實際上不是數字,而是軸線,令人困惑。 – xApple