2017-07-18 70 views
0

我被使用'figsize'改變劇情的大小難住了。也許我不完全理解matplotlib的工作原理。下面的代碼來自我早期的一個問題,即關於使用兩個座標軸在一個圖中繪製不同比例的系列。 Prin_Balances是一個由浮點數組成的數據框,列名應該是自解釋的並且對應於要素。調整圖的大小

fig, ax = plt.subplots() 
# plt.figure(figsize=(9,6)) 

plt.xticks(rotation=45) 
plt.plot(Prin_Balances['UPB'], '--r', label='UPB') 
plt.legend() 
ax.tick_params('Bal', colors='r') 


# Get second axis 
ax2 = ax.twinx() 
plt.plot(Prin_Balances['1 Mos'], label='1 Mos', color = 'blue') 
plt.plot(Prin_Balances['2 Mos'], label='2 Mos', color = 'green') 
plt.plot(Prin_Balances['3 Mos'], label='3 Mos', color = 'yellow') 
plt.plot(Prin_Balances['> 3 Mos'], label='>3 Mos', color = 'purple') 
plt.legend() 

ax.tick_params('vals', colors='b') 

現在,當我運行這段代碼,我得到一個不錯的,但小圖:

enter image description here

我怎樣才能改變這種圖形的大小?我調用第二行('plt.figure')的代碼中的點似乎對輸出有影響,在某些情況下,在具有不同標籤的新圖上繪製空框。 (我還沒有包括這一個鏡頭,因爲我使用Juypter筆記本電腦和兩個圖表是隻能由輸出窗口向下滾動查看

+0

https://stackoverflow.com/questions/332289/how-do-you-change-the-size-of-figures-drawn-with-matplotlib – Raf

+0

在上面的答案中,請注意有兩種方法,一種是您在代碼中使用的方法,'figure(figsize = size)',另一種方法是更改​​'rcParams'。我更喜歡使用'rcParams',因爲它適用於所有地塊。 – Raf

+0

謝謝!這麼簡單的監督。這讓我瘋狂。 – GPB

回答

-1

嘗試是這樣的:

fig = plt.figure(figsize=(9,6)) 
ax = fig.add_subplot(121) 

# ax.xticks(rotation=45) 
ax.plot(Prin_Balances['UPB'], '--r', label='UPB') 
ax.legend() 
ax.tick_params('Bal', colors='r') 


# Get second axis 
ax2 = fig.add_subplot(122) 
ax2.plot(Prin_Balances['1 Mos'], label='1 Mos', color = 'blue') 
ax2.plot(Prin_Balances['2 Mos'], label='2 Mos', color = 'green') 
ax2.plot(Prin_Balances['3 Mos'], label='3 Mos', color = 'yellow') 
ax2.plot(Prin_Balances['> 3 Mos'], label='>3 Mos', color = 'purple') 
ax2.legend() 
ax2.tick_params('vals', colors='b') 

plt.show() 

ax是一個AxesSubplot,它沒有xticks()方法,所以你必須找到相應的。(set_xticks()的作品,但不能與rotation關鍵字,從我可以告訴。)

+0

我有興趣調整上面的代碼,而不是xticks問題。 – GPB

+0

查看上面的Raf評論,這解決了我的問題。 – GPB

+0

我提供的代碼可以讓你調整你的情節。它使用matplotlib的OOP接口,它比你的代碼中使用的狀態機接口更強大/更靈活。 – eeshugerman