2012-11-13 127 views
31

有沒有簡單的方法將y軸標籤放在圖的右側?我知道可以使用ax.yaxis.tick_right()來完成對刻度標籤的操作,但是我想知道是否也可以爲軸標籤完成。其中浮現在腦海matplotlib右側的y軸標籤

一個想法是使用

ax.yaxis.tick_right() 
ax2 = ax.twinx() 
ax2.set_ylabel('foo') 

然而,這並沒有把所有的標籤(打勾和軸標籤)在右側的理想效果,同時保留y軸的範圍。總之,我想要一種將所有y軸標籤從左向右移動的方法。

回答

55

看起來你可以做到這一點:

ax.yaxis.set_label_position("right") 

爲例見here

+0

這樣做。 – Thucydides411

5

如果你想跟隨matplotlib給出的例子,並創建與軸的兩側標籤的人物,但不必使用subplots()功能,這裏是我的解決方案:

from matplotlib import pyplot as plt 
import numpy as np 

ax1 = plt.plot() 
t = np.arange(0.01, 10.0, 0.01) 
s1 = np.exp(t) 
plt.plot(t,s1,'b-') 
plt.xlabel('t (s)') 
plt.ylabel('exp',color='b') 

ax2 = ax1.twinx() 
s2 = np.sin(2*np.pi*t) 
ax2.plot(t, s2, 'r.') 
plt.ylabel('sin', color='r') 
plt.show() 

+1

這並沒有爲我工作:在'' AX2 = ax1.twinx() '文件 「prueba.py」,第11行,'' AttributeError的: '名單' 對象有沒有屬性「twinx'' –

+1

嘗試plt.gca()。twinx() –