簡體   English   中英

如何消除matplotlib中子圖之間的間隙?

[英]How to remove gaps between subplots in matplotlib?

下面的代碼在子圖之間產生間隙。 如何消除子圖之間的間隙並使圖像成為緊密網格?

在此處輸入圖片說明

import matplotlib.pyplot as plt

for i in range(16):
    i = i + 1
    ax1 = plt.subplot(4, 4, i)
    plt.axis('on')
    ax1.set_xticklabels([])
    ax1.set_yticklabels([])
    ax1.set_aspect('equal')
    plt.subplots_adjust(wspace=None, hspace=None)
plt.show()

問題是使用aspect='equal' ,它可以防止子圖拉伸到任意縱橫比並填滿所有空白空間。

通常,這會起作用:

import matplotlib.pyplot as plt

ax = [plt.subplot(2,2,i+1) for i in range(4)]

for a in ax:
    a.set_xticklabels([])
    a.set_yticklabels([])

plt.subplots_adjust(wspace=0, hspace=0)

結果是這樣的:

但是,使用aspect='equal' ,如下面的代碼所示:

import matplotlib.pyplot as plt

ax = [plt.subplot(2,2,i+1) for i in range(4)]

for a in ax:
    a.set_xticklabels([])
    a.set_yticklabels([])
    a.set_aspect('equal')

plt.subplots_adjust(wspace=0, hspace=0)

這是我們得到的:

第二種情況的不同之處在於您已強制 x 軸和 y 軸具有相同數量的單位/像素。 由於默認情況下軸從 0 到 1(即,在繪制任何內容之前),使用aspect='equal'強制每個軸為正方形。 由於圖形不是正方形,因此 pyplot 在水平軸之間增加了額外的間距。

要解決此問題,您可以將圖形設置為具有正確的縱橫比。 我們將在這里使用面向對象的 pyplot 接口,我認為它總體上是優越的:

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(8,8)) # Notice the equal aspect ratio
ax = [fig.add_subplot(2,2,i+1) for i in range(4)]

for a in ax:
    a.set_xticklabels([])
    a.set_yticklabels([])
    a.set_aspect('equal')

fig.subplots_adjust(wspace=0, hspace=0)

結果如下:

您可以使用gridspec來控制軸之間的間距。 這里有更多信息

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

plt.figure(figsize = (4,4))
gs1 = gridspec.GridSpec(4, 4)
gs1.update(wspace=0.025, hspace=0.05) # set the spacing between axes. 

for i in range(16):
   # i = i + 1 # grid spec indexes from 0
    ax1 = plt.subplot(gs1[i])
    plt.axis('on')
    ax1.set_xticklabels([])
    ax1.set_yticklabels([])
    ax1.set_aspect('equal')

plt.show()

軸非常靠近

在不完全使用gridspec的情況下,以下內容也可用於通過將wspacehspace設置為零來消除間隙:

import matplotlib.pyplot as plt

plt.clf()
f, axarr = plt.subplots(4, 4, gridspec_kw = {'wspace':0, 'hspace':0})

for i, ax in enumerate(f.axes):
    ax.grid('on', linestyle='--')
    ax.set_xticklabels([])
    ax.set_yticklabels([])

plt.show()
plt.close()

導致:

.

使用最近的 matplotlib 版本,您可能想嘗試Constrained Layout 但是,這確實(或至少確實)不適用於plt.subplot() ,因此您需要改用plt.subplots()

fig, axs = plt.subplots(4, 4, constrained_layout=True)

你試過plt.tight_layout()嗎?

plt.tight_layout()在此處輸入圖片說明 沒有它:在此處輸入圖片說明

或者:像這樣(使用add_axes

left=[0.1,0.3,0.5,0.7]
width=[0.2,0.2, 0.2, 0.2]
rectLS=[]
for x in left:
   for y in left:
       rectLS.append([x, y, 0.2, 0.2])
axLS=[]
fig=plt.figure()
axLS.append(fig.add_axes(rectLS[0]))
for i in [1,2,3]:
     axLS.append(fig.add_axes(rectLS[i],sharey=axLS[-1]))    
axLS.append(fig.add_axes(rectLS[4]))
for i in [1,2,3]:
     axLS.append(fig.add_axes(rectLS[i+4],sharex=axLS[i],sharey=axLS[-1]))
axLS.append(fig.add_axes(rectLS[8]))
for i in [5,6,7]:
     axLS.append(fig.add_axes(rectLS[i+4],sharex=axLS[i],sharey=axLS[-1]))     
axLS.append(fig.add_axes(rectLS[12]))
for i in [9,10,11]:
     axLS.append(fig.add_axes(rectLS[i+4],sharex=axLS[i],sharey=axLS[-1]))

如果您不需要共享軸,那么只需axLS=map(fig.add_axes, rectLS)在此處輸入圖片說明

另一種方法是使用plt.subplots_adjust()pad關鍵字,它也接受負值:

import matplotlib.pyplot as plt

ax = [plt.subplot(2,2,i+1) for i in range(4)]

for a in ax:
    a.set_xticklabels([])
    a.set_yticklabels([])

plt.subplots_adjust(pad=-5.0)

此外,要刪除所有子圖(即畫布)外邊緣的白色,請始終使用plt.savefig(fname, bbox_inches="tight")

MERose 的答案是正確的,但您可以使用它,如下所示:

plt.tight_layout(pad=1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM