2013-07-22 33 views
8

使用熊貓在I-Python Notebook中進行繪圖,我有幾個繪圖,而且由於Matplotlib決定Y軸的設置不同,我們需要使用相同的範圍來比較這些數據。 我已經嘗試了幾種變體:(我假設我需要將限制應用到每個plot ..但是因爲我無法獲得一個工作...從Matplotlib文檔看來,我需要設置ylim,但想不通的語法這樣做。使用熊貓在Matplotlib中設置Y軸

df2250.plot(); plt.ylim((100000,500000)) <<<< if I insert the ; I get int not callable and if I leave it out I get invalid syntax. anyhow, neither is right... 
df2260.plot() 
df5.plot() 

回答

23

熊貓圖()返回的軸,你可以用它來設置ylim就可以了。

ax1 = df2250.plot() 
ax2 = df2260.plot() 
ax3 = df5.plot() 

ax1.set_ylim(100000,500000) 
ax2.set_ylim(100000,500000) 
etc... 

您也可以通過一個軸來熊貓的情節,所以在同一軸上繪圖可以這樣做:

ax1 = df2250.plot() 
df2260.plot(ax=ax1) 
etc... 

如果你想了很多不同的地塊,上定義的正手和一個圖中的軸可能是一個解決方案,讓你最大程度控制:

fig, axs = plt.subplots(1,3,figsize=(10,4), subplot_kw={'ylim': (100000,500000)}) 

df2260.plot(ax=axs[0]) 
df2260.plot(ax=axs[1]) 
etc... 
+0

完美,太謝謝你了! – dartdog

+1

您也可以將'sharey = True'添加到'plt.subplots'。然後,即使在放大某個特定的子圖上時,y限制也會保持不變。 – esmit

+0

確實,好點! –