簡體   English   中英

Matplotlib:如何分隔圖中的最后一個子圖線和最后一個子圖列?

[英]Matplotlib : How to separate the last subplot line and the last subplot column in a figure?

使用 matplotlib,我繪制了 nxn 個子圖(本例中 n=8):

fig, ax = plt.subplots(8,8, figsize=(18,10), sharex='col', sharey='row')
fig.subplots_adjust(hspace=0, wspace=0)

在此處輸入圖像描述

我想將最后一行和最后一列分開以獲得如下內容:

在此處輸入圖像描述

我必須將 Gridspec (如答案https://stackoverflow.com/a/54747473/7462275中的答案)與for循環一起使用,因為n是一個參數還是有更簡單的解決方案? 感謝您的回答。

編輯1

在回答https://stackoverflow.com/a/54747473/7462275之后,我編寫了這段代碼。

import matplotlib
n=7
m=5
outer_gs = matplotlib.gridspec.GridSpec(2,2, height_ratios=[n,1], width_ratios=[m,1], hspace=0.1, wspace=0.1)
inner_gs = matplotlib.gridspec.GridSpecFromSubplotSpec(n,m, subplot_spec=outer_gs[0,0], hspace=0, wspace=0)
line_gs = matplotlib.gridspec.GridSpecFromSubplotSpec(n,1, subplot_spec=outer_gs[0,1], hspace=0, wspace=0.1)
col_gs = matplotlib.gridspec.GridSpecFromSubplotSpec(1,m, subplot_spec=outer_gs[1,0], hspace=0.1, wspace=0)
ax_11=[]
ax_12=[]
ax_21=[]
ax_22=[]
fig = plt.figure(figsize=(18,10))
for i in range (n):
    ax_11.append([])
    for j in range(m):
        ax_11[i].append(fig.add_subplot(inner_gs[i,j]))
        
for i in range(n):
        ax_12.append(fig.add_subplot(line_gs[i,0]))
        
for i in range(m):
        ax_21.append(fig.add_subplot(col_gs[0,i]))

ax_22 = fig.add_subplot(outer_gs[1,1])

我得到了在此處輸入圖像描述

但是,現在我必須刪除內軸標簽並只保留外軸標簽。

編輯2

它適用於以下代碼:

import matplotlib
n=7
m=5
outer_gs = matplotlib.gridspec.GridSpec(2,2, height_ratios=[n,1], width_ratios=[m,1], hspace=0.1, wspace=0.1)
inner_gs = matplotlib.gridspec.GridSpecFromSubplotSpec(n,m, subplot_spec=outer_gs[0,0], hspace=0, wspace=0)
line_gs = matplotlib.gridspec.GridSpecFromSubplotSpec(n,1, subplot_spec=outer_gs[0,1], hspace=0, wspace=0.1)
col_gs = matplotlib.gridspec.GridSpecFromSubplotSpec(1,m, subplot_spec=outer_gs[1,0], hspace=0.1, wspace=0)
ax_11=[]
ax_12=[]
ax_21=[]
ax_22=[]
fig = plt.figure(figsize=(18,10))
for i in range (n):
    ax_11.append([])
    for j in range(m):
        ax_11[i].append(fig.add_subplot(inner_gs[i,j]))
        if not ax_11[i][j].get_subplotspec().is_first_col():
            ax_11[i][j].axes.yaxis.set_visible(False)
        ax_11[i][j].axes.xaxis.set_visible(False)
        
for i in range(n):
        ax_12.append(fig.add_subplot(line_gs[i,0]))
        ax_12[i].axes.yaxis.set_visible(False)
        ax_12[i].axes.xaxis.set_visible(False)
        
for i in range(m):
        ax_21.append(fig.add_subplot(col_gs[0,i]))
        if not ax_21[i].get_subplotspec().is_first_col():
            ax_21[i].axes.yaxis.set_visible(False)

ax_22 = fig.add_subplot(outer_gs[1,1])
ax_22.axes.yaxis.set_visible(False);

在此處輸入圖像描述

但是,我想知道獲得這樣的數字是否是最簡單的方法。 我正在發現 matplotlib,我不確定這段代碼是否很好,即使它給出了預期的結果。

根據這個答案文檔,您可以使用 tick_params 更改刻度和刻度標簽的外觀。

我在您的代碼中添加了一些 if 語句和 tick_params。

import matplotlib.pyplot as plt
import matplotlib
n=7
m=5
outer_gs = matplotlib.gridspec.GridSpec(2,2, height_ratios=[n,1],
width_ratios=
[m,1], hspace=0.1, wspace=0.1)
inner_gs = matplotlib.gridspec.GridSpecFromSubplotSpec(n,m, 
subplot_spec=outer_gs[0,0], hspace=0, wspace=0)
line_gs = matplotlib.gridspec.GridSpecFromSubplotSpec(n,1, 
subplot_spec=outer_gs[0,1], hspace=0, wspace=0.1)
col_gs = matplotlib.gridspec.GridSpecFromSubplotSpec(1,m, 
subplot_spec=outer_gs[1,0], hspace=0.1, wspace=0)
ax_11=[]
ax_12=[]
ax_21=[]
ax_22=[]
fig = plt.figure(figsize=(18,10))
for i in range (n):
    ax_11.append([])
    for j in range(m):
        ax_11[i].append(fig.add_subplot(inner_gs[i,j]))
        plt.tick_params(labelbottom=False, length=0)
        if j!=0:
            plt.tick_params(labelleft=False, length=0)

for i in range(n):
        ax_12.append(fig.add_subplot(line_gs[i,0]))
        plt.tick_params(labelleft=False, length=0)
        plt.tick_params(labelbottom=False, length=0)

for i in range(m):
    ax_21.append(fig.add_subplot(col_gs[0,i]))
    if i!=0:
        plt.tick_params(labelleft=False, length=0)

ax_22 = fig.add_subplot(outer_gs[1,1])
plt.tick_params(labelleft=False, length=0)

plt.show()

這是結果。 在此處輸入圖像描述

另一種方法是繼續使用plt.subplots()創建軸,但為間隙添加額外的列和行,您可以在創建后將其刪除。

請注意,此方法意味着您的軸將繼續像以前一樣共享,並且無需為特定軸打開或關閉刻度標簽。

使用gridspec_kwheight_ratioswidth_ratios選項,我們可以控制這些間隙的高度和寬度。 在下面的示例中,我將它們設置為常規軸尺寸的五分之一,但這很容易根據您的需要進行更改。

我們可以使用.remove()方法刪除虛擬軸。 然后它們不會妨礙我們的ax數組,我們可以使用numpy.delete刪除這些切片。

例如:

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots(9, 9, figsize=(18, 10), sharex='col', sharey='row',
                       gridspec_kw={
                           'height_ratios': (1, 1, 1, 1, 1, 1, 1, 0.2, 1), 
                           'width_ratios': (1, 1, 1, 1, 1, 1, 1, 0.2, 1),
                           'hspace': 0, 'wspace': 0
                           })

# remove 8th column
for dax in ax[:, 7]:
    dax.remove()
ax = np.delete(ax, 7, 1)

# remove 8th row
for dax in ax[7, :]:
    dax.remove()
ax = np.delete(ax, 7, 0)

plt.show()

在此處輸入圖像描述

暫無
暫無

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

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