如何从子图中完全删除图并正确调整大小?

Posted

技术标签:

【中文标题】如何从子图中完全删除图并正确调整大小?【英文标题】:How to fully delete plots from subplot and properly resize? 【发布时间】:2020-10-23 13:35:48 【问题描述】:

我正在尝试为即将发表的论文创建一个角落图,但我遇到了困难。我正在创建一个 N x N 子图数组(当前,N = 6),然后删除其中一半以上。问题是,在我删除无关的子图后,该图似乎并没有自行调整大小,因此当我稍后使用虚拟子图添加图例时,它存在于已删除子图的整行和整列所在的区域中,因此扩大了图。我已经为此工作了几个小时,但还没有找到解决方案。这是 MWE:

import matplotlib.pyplot as plt
%matplotlib notebook

n_char = 8

# Set up the main figure.
fig, ax = plt.subplots(n_char, n_char, figsize=(n_char, n_char))

# Get rid of the axis labels unless it's on the left-most column or bottom-most row.
for i in range(0, n_char):

    # For each row, loop over each column.
    for j in range(0, n_char):

        # If the plot isn't in the bottom-most row, get rid of the x-axis tick labels.
        if i != n_char - 1:
            ax[i, j].set_xticklabels([])

        # If the plot isn't in the left-most column, get rid of the y-axis tick labels.
        if j != 0:
            ax[i, j].set_yticklabels([])

# Remove the plots that are repetitive or boring (plotting against the same characteristic).
for i in range(0, n_char):

    # For each row, loop over each column.
    for j in range(0, n_char):

        # Delete the offending axes.
        if j >= i:
            ax[i, j].remove()

# Set the spacing between the plots to a much smaller value.
fig.subplots_adjust(hspace=0.00, wspace=0.00)

# Create a big plot for the legend.  Have the frame hidden.
fig.add_subplot(111, frameon=False, xticks=[], yticks=[], xticklabels=[], yticklabels=[])

# Create some dummy data to serve as the source of the legend.
plt.scatter([10], [10], color="k", s=5, zorder=2, label="Targets")

# Set the x-axis limits such that the dummy data point is invisible.
fig.gca().set_xlim(-1, 1)

# Add the legend to the plot.  Have it located in the upper right.
plt.legend(scatterpoints=1, loc="upper right", fontsize=5)

# Save the final plot.
fig.savefig("./../Code Output/Other Plots/Corner_Plot_Test.png", bbox_inches="tight", dpi=500)

我在 Stack Overflow 上查看了许多不同的问题。两个最有希望的候选人是this one,但我发现由于大量的地块,该解决方案不太可行(坦率地说,我并没有完全理解该解决方案)。我认为this one 中的第一个答案也可能有效,因为我认为这是一个大小问题(即图形没有调整大小,因此创建一个新的子图就是创建一个与原始图形大小相同的子图),但所有这一切did 是调整整个图形的大小,所以这也不起作用。

为了提供帮助,我还会附上一张图片。我获取了上面代码的输出并对其进行了编辑以显示我想要的内容:

我应该补充一点,如果我添加子图,则输出与我预期的一样(即大小合适),因此添加子图时会出现问题,即行fig.add_subplot(111, frameon=False, xticks=[], yticks=[], xticklabels=[], yticklabels=[]).

【问题讨论】:

在决定排除或保留哪些子图之前,您是否需要调用子图(以及所有其他 matplotlib 内容)? 我不确定这是否是您发表评论的方向,但我现在有一个可以按我想要的方式工作的解决方案。我基本上只是根本没有添加虚拟行和列,而是编辑了数据的绘制方式。由于该行和列从未存在,因此该图现在是我想要的大小,并且图例位于正确的位置。 是的,我就是这么想的。无论如何,很好,你的问题解决了! 【参考方案1】:

使用GridSpec 可能会有所帮助。 GridSpec 用于指定要绘制的轴数组。您可以在选项中将列的宽度和行的高度设置为比率。不需要的行的高度比应该很小,而不需要的列的宽度比应该很小。 这是可运行的代码和输出图:-

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
#import numpy as np

fig = plt.figure(figsize=(8, 8))
nn = 6

# will create gridspec of 6 rows, 6 columns
# 1st row will occupy v small heights
# last column will occupy v small widths

sm = 0.01            # the v small width/height
wh = (1.-sm)/(nn-1.) # useful width/height

gs = gridspec.GridSpec(nn, nn, width_ratios=[*[wh]*(nn-1), sm], \
                      height_ratios= [sm, *[wh]*(nn-1)])

cols, rows = nn, nn
ax = [[0 for i in range(cols)] for j in range(rows)] 

for ea in range(nn):
    for eb in range(nn):
        ax[ea][eb] = fig.add_subplot(gs[ea, eb])
        ax[ea][eb].set_xticklabels([])
        ax[ea][eb].set_yticklabels([])
        if eb>=ea:
            ax[ea][eb].remove()

# plot data on some axes
# note that axes on the first row (index=0) are gone
ax[2][0].plot([2,5,3,7])
ax[4][2].plot([2,3,7])

# make legend in upper-right axes (GridSpec's first row, last column)
# first index: 0
# second index: nn-1
rx, cx = 0, nn-1
ax[rx][cx] = fig.add_subplot(gs[rx,cx])
hdl = ax[rx][cx].scatter([10], [10], color="k", s=5, zorder=2, label="Targets")
ax[rx][cx].set_axis_off()
#ax[rx][cx].set_visible(True)  # already True
ax[rx][cx].set_xticklabels([])
ax[rx][cx].set_yticklabels([])

# plot legend
plt.legend(bbox_to_anchor=(1.0, 1.0), loc='upper right', borderaxespad=0.)

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

plt.show

【讨论】:

您提供的代码没有运行。当我运行它时,对于gs = gridspec.GridSpec(nn, nn, width_ratios=[*[wh]*(nn-1), sm], \ height_ratios= [sm, *[wh]*(nn-1)]) 行会抛出一个无效的语法错误,特别是[*[wh]*(nn-1), sm] 中的第一个*。删除这两个 * 并运行会导致此错误:+ 的不支持的操作数类型:'float' 和 'list' @mknote 我使用 Python 3.7 和 matplotlib 版本 3.1.1。你还在使用 Python 2.7 吗? 尝试将*[wh]*(nn-1) 更改为0.198、0.198、0.198、0.198、0.198 和*[wh]*(nn-1) 更改为0.198、0.198、0.198、0.198、0.198 我其实还在2.7,你提供的更正允许代码运行。输出与您的输出不完全相同,但非常相似并且确实提供了问题的解决方案。碰巧的是,与此同时,我想出了另一个解决方案,我认为我最终会使用它(我在对原始问题的评论中概述了它),但由于这确实解决了问题,我将接受它作为解决方案。

以上是关于如何从子图中完全删除图并正确调整大小?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 matplotlib 调整子图中的图形大小

真正从 <textarea> 中删除调整大小句柄

当鼠标悬停在边框上时,如何完全禁用调整窗口大小,包括调整大小图标?

如何在 ggdensity() 中调整绘图标题的大小?

无法正确调整 .bmp 图像的大小(CS50 pset 4,调整大小,不太舒服)

使用 PIL.resize 调整 JPG 大小会得到一个完全黑色的图像