2015-12-01 139 views
3

在matplotlib,我知道如何設置一個數字的高度和寬度,DPI:使用matplotlib中的plt.subplots設置圖形的高度和寬度?

fig = plt.figure(figsize=(4, 5), dpi=100) 

然而,似乎如果我想創建多個小地塊,我不能創造這樣的數字,我必須使用這個:

fig, subplots = plt.subplots(nrows=4, ncols=4) 

如何設置這樣的子圖創建的圖形的高度和寬度以及DPI?

+0

您是否嘗試過'gridspec'模塊? – salomonvh

+0

所有額外的kwargs到'subplots'都會通過調用'figure' http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.subplots – tacaswell

回答

2

一個工作gridspec模塊的例子:

import matplotlib.pyplot as plt 
from matplotlib import gridspec 

fig = plt.figure(figsize=(18,18)) 

gs = gridspec.GridSpec(3, 3) 

ax1 = fig.add_subplot(gs[0,:]) 
ax1.plot([1,2,3,4,5], [10,5,10,5,10], 'r-') 

ax2 = fig.add_subplot(gs[1,:-1]) 
ax2.plot([1,2,3,4], [1,4,9,16], 'k-') 

ax3 = fig.add_subplot(gs[1:, 2]) 
ax3.plot([1,2,3,4], [1,10,100,1000], 'b-') 

ax4 = fig.add_subplot(gs[2,0]) 
ax4.plot([1,2,3,4], [0,0,1,1], 'g-') 

ax5 = fig.add_subplot(gs[2,1]) 
ax5.plot([1,2,3,4], [1,0,0,1], 'c-') 

gs.update(wspace=0.5, hspace=0.5) 

plt.show() 

但我更喜歡在一個函數進行包裝,並使用它像這樣:

def mySubplotFunction(fig,gs,x,y,c,ax=None): 

    if not ax: 
     ax = fig.add_subplot(gs) 
    ax.plot(x, y, c) 

    return fig, ax 

用法:

fig2 = plt.figure(figsize=(9,9)) 
fig2, ax1 = mySubplotFunction(fig2,gs[0,:],[1,2,3,4,5],[10,5,10,5,10],'r-'); 
fig2, ax2 = mySubplotFunction(fig2,gs[1,:-1],[1,2,3,4],[1,4,9,16],'k-'); 
0

你實際上可以指定高度和widthplt.savefig('Desktop/test.png',dpi = 500) ,即使它不列爲幫助(我認爲這是傳遞到數字電話()?)關鍵字:

fig,axs=plt.subplots(nrows,ncols,figsize=(width,height)) 

出於某種原因,dpi值雖然忽略。但是,當保存圖時,可以使用它,當它很重要時:

plt.savefig('test.png',dpi=1000) 
相關問題