2015-04-21 38 views
1

我似乎無法獲得子圖之間的水平差距消失。有什麼建議麼?matplotlib:刪除軸之間的水平間隙?

代碼:

plt.clf() 
fig = plt.figure() 
for i in range(6): 
    ax = fig.add_subplot(3,2,i) 

    frame_range = [[]] 

    ax.set_xlim(-100000, 1300000) 
    ax.set_ylim(8000000, 9100000) 
    ax.set_aspect(1) 
    ax.set_xticks([]) 
    ax.set_yticks([]) 
    ax.set_frame_on(False) 

    ax.add_patch(dt.PolygonPatch(provs[0],fc = 'None', ec = 'black')) 


fig.tight_layout(pad=0, w_pad=0, h_pad=0) 
plt.subplots_adjust(wspace=0, hspace=0) 

plt.savefig(wd + 'example.png') 

例子貼都爲這個代碼,並與蜱和幀留在

Example

Example 2

+1

可能重複[如何matplotlib次要情節之間移除差距?](http://stackoverflow.com/questions/20057260/how-to-remove-gaps-between-subplots -in-matplotlib) – cphlewis

回答

2

您設置兩個並行的規則,你的圖表。

一個是軸方面

ax.set_aspect(i) 

這將迫使情節始終尊重1:1的比例。

另一個是將h_spacew_space設置爲零。在這種情況下,matplotlib將嘗試更改軸的大小以將空間減少到零。當您將邊緣設置爲1時,只要其中一條邊相互接觸,軸的大小就不會再改變。這產生了保持圖形水平分開的間隙。

有兩種方法可以迫使它們彼此接近。

  • 您可以更改圖形寬度以使它們彼此靠得更近。
  • 您可以設置左右邊緣的間距,使它們彼此靠得更近。

使用你給出的例子,我修改了幾行來說明左右間距可以做什麼。

fig = plt.figure() 
for i in range(6): 
    ax = fig.add_subplot(3,2,i) 
    ax.plot(linspace(-1,1),sin(2*pi*linspace(-1,1))) 
    draw() 
    frame_range = [[]] 

    ax.set_aspect(1) 
    ax.set_xticks([]) 
    ax.set_yticks([]) 
# ax.set_frame_on(False) 
# ax.add_patch(dt.PolygonPatch(provs[0],fc = 'None', ec = 'black')) 


fig.tight_layout(pad=0,w_pad=0, h_pad=0) 
subplots_adjust(left=0.25,right=0.75,wspace=0, hspace=0) 

結果應該像下圖一樣。 請務必記住,如果調整窗口大小,繪圖將再次分開,這取決於是否使其更寬/更窄/更短。

希望它可以幫助 Grid distribution