关闭子图中的轴

发布于 2025-01-18 21:18:28 字数 920 浏览 4 评论 0 原文

我有以下代码:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.cm as cm

img = mpimg.imread("lena.jpg")

fig, axs = plt.subplots(2, 2)
axs[0,0].imshow(img, cmap = cm.Greys_r)
axs[0,0].set_title("Rank = 512")

rank = 128
new_img = prune_matrix(rank, img)
axs[0,1].imshow(new_img, cmap = cm.Greys_r)
axs[0,1].set_title("Rank = %s" %rank)

rank = 32
new_img = prune_matrix(rank, img)
axs[1,0].imshow(new_img, cmap = cm.Greys_r)
axs[1,0].set_title("Rank = %s" %rank)

rank = 16
new_img = prune_matrix(rank, img)
axs[1,1].imshow(new_img, cmap = cm.Greys_r)
axs[1,1].set_title("Rank = %s" %rank)

plt.show()

但是,由于轴上的值,结果非常难看:

2x2 subplots

我怎样才能同时关闭所有子图的轴值?

如何删除轴、图例和白色填充不起作用,因为我不知道如何使它与次要情节。

I have the following code:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.cm as cm

img = mpimg.imread("lena.jpg")

fig, axs = plt.subplots(2, 2)
axs[0,0].imshow(img, cmap = cm.Greys_r)
axs[0,0].set_title("Rank = 512")

rank = 128
new_img = prune_matrix(rank, img)
axs[0,1].imshow(new_img, cmap = cm.Greys_r)
axs[0,1].set_title("Rank = %s" %rank)

rank = 32
new_img = prune_matrix(rank, img)
axs[1,0].imshow(new_img, cmap = cm.Greys_r)
axs[1,0].set_title("Rank = %s" %rank)

rank = 16
new_img = prune_matrix(rank, img)
axs[1,1].imshow(new_img, cmap = cm.Greys_r)
axs[1,1].set_title("Rank = %s" %rank)

plt.show()

However, the result is pretty ugly because of the values on the axes:

2x2 subplots

How can I turn off axes values for all subplots simultaneously?

How to remove axis, legends, and white padding doesn't work because I don't know how to make it work with subplots.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凶凌 2025-01-25 21:18:28
  • You can turn the Axes off by following the advice in Veedrac's comment (linking to
  • 而不是使用 ,使用 其中 ax matplotlib.axes 对象。


  • 下面的代码显示了没有 prune_matrix 的结果,这是不可用的。
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.cm as cm
import matplotlib.cbook as cbook  # used for matplotlib sample image

# load readily available sample image
with cbook.get_sample_data('grace_hopper.jpg') as image_file:
    img = plt.imread(image_file)

# read a local file
# img = mpimg.imread("file.jpg")

fig, axs = plt.subplots(nrows=2, ncols=2, figsize=(8, 8), tight_layout=True)
axs[0, 0].imshow(img, cmap=cm.Greys_r)
axs[0, 0].set_title("Rank = 512")
axs[0, 0].axis("off")

axs[0, 1].imshow(img, cmap=cm.Greys_r)
axs[0, 1].set_title("Rank = %s" % 128)
axs[0, 1].axis("off")

axs[1, 0].imshow(img, cmap=cm.Greys_r)
axs[1, 0].set_title("Rank = %s" % 32)
axs[1, 0].axis("off")

axs[1, 1].imshow(img, cmap=cm.Greys_r)
axs[1, 1].set_title("Rank = %s" % 16)
axs[1, 1].axis("off")

plt.show()

注意:仅关闭X或Y轴,您可以使用 set_visible() eg:

axs[0, 0].xaxis.set_visible(False) # Hide only x axis

  • 迭代方法:
fig, axs = plt.subplots(nrows=2, ncols=2, figsize=(8, 8), tight_layout=True)

# convert the 2d array to 1d, which removes the need to iterate through i and j
axs = axs.flat
ranks = [512, 128, 32, 16]

# iterate through each Axes with the associate rank
for ax, rank in zip(axs, ranks):

    ax.imshow(img, cmap=cm.Greys_r)
    ax.set_title(f'Rank = {rank}')
    ax.axis('off')

plt.show()
  • You can turn the Axes off by following the advice in Veedrac's comment (linking to here) with one small modification.
  • Rather than using plt.axis('off'), use ax.axis('off') where ax is a matplotlib.axes object.
    • To do this, index each Axes, axs[0, 0].axis('off'), and so on for each subplot.
    • See Native Matplotlib interfaces for the difference between pyplot and Axes.
  • The code below shows the result without the prune_matrix, which is not available.
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.cm as cm
import matplotlib.cbook as cbook  # used for matplotlib sample image

# load readily available sample image
with cbook.get_sample_data('grace_hopper.jpg') as image_file:
    img = plt.imread(image_file)

# read a local file
# img = mpimg.imread("file.jpg")

fig, axs = plt.subplots(nrows=2, ncols=2, figsize=(8, 8), tight_layout=True)
axs[0, 0].imshow(img, cmap=cm.Greys_r)
axs[0, 0].set_title("Rank = 512")
axs[0, 0].axis("off")

axs[0, 1].imshow(img, cmap=cm.Greys_r)
axs[0, 1].set_title("Rank = %s" % 128)
axs[0, 1].axis("off")

axs[1, 0].imshow(img, cmap=cm.Greys_r)
axs[1, 0].set_title("Rank = %s" % 32)
axs[1, 0].axis("off")

axs[1, 1].imshow(img, cmap=cm.Greys_r)
axs[1, 1].set_title("Rank = %s" % 16)
axs[1, 1].axis("off")

plt.show()

enter image description here

Note: To turn off only the x or y axis you can use set_visible() e.g.:

axs[0, 0].xaxis.set_visible(False) # Hide only x axis

  • Iterative approach
fig, axs = plt.subplots(nrows=2, ncols=2, figsize=(8, 8), tight_layout=True)

# convert the 2d array to 1d, which removes the need to iterate through i and j
axs = axs.flat
ranks = [512, 128, 32, 16]

# iterate through each Axes with the associate rank
for ax, rank in zip(axs, ranks):

    ax.imshow(img, cmap=cm.Greys_r)
    ax.set_title(f'Rank = {rank}')
    ax.axis('off')

plt.show()
不一样的天空 2025-01-25 21:18:28

给定:

import matplotlib.pyplot as plt

fig, axs = plt.subplots(2, 2)

要关闭所有子图的轴:

for ax in axs.ravel():
    ax.set_axis_off()

Given:

import matplotlib.pyplot as plt

fig, axs = plt.subplots(2, 2)

To turn off axes for all subplots:

for ax in axs.ravel():
    ax.set_axis_off()
ペ泪落弦音 2025-01-25 21:18:28

另一种可能的方法是在绘制每个轴时将它们的 axison 属性设置为 False。

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

img = mpimg.imread("Stewie_Griffin.png")

fig, axs = plt.subplots(2, 2)

for i, lst in enumerate([[512, 128], [32, 16]]):
    for j, rank in enumerate(lst):
        axs[i,j].imshow(img)
        axs[i,j].set_title(f"Rank = {rank}")
        axs[i,j].axison = False           # <---- remove axis

img1

如果您想清楚地看到删除的内容,可以使用 Axes.set() 分别“删除”框架和刻度。

        axs[i,j].set(frame_on=False, xticks=[], yticks=[])

最后,如果您想在绘制图形后删除框架和刻度,您可以循环遍历图形本身中的轴列表。

for ax in fig.axes:
    ax.axison = False

注意此处给出的所有三个方法(axis('off')set_axis_off()axison=False)都是等效的方法,因为在底层, axis('off') 调用 set_axis_off(),后者又执行 axison=False,因此最终它们是相同的。

Another possible way is to set the axison attribute to False for each Axes as they get plotted.

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

img = mpimg.imread("Stewie_Griffin.png")

fig, axs = plt.subplots(2, 2)

for i, lst in enumerate([[512, 128], [32, 16]]):
    for j, rank in enumerate(lst):
        axs[i,j].imshow(img)
        axs[i,j].set_title(f"Rank = {rank}")
        axs[i,j].axison = False           # <---- remove axis

img1

If you want to clearly see what is removed, you can "remove" frames and ticks separately using Axes.set().

        axs[i,j].set(frame_on=False, xticks=[], yticks=[])

Finally, if you want to remove the frames and ticks after the graphs are plotted, you can loop over the list of axes in the figure itself.

for ax in fig.axes:
    ax.axison = False

N.B. All three methods given here (axis('off'), set_axis_off() and axison=False) are equivalent methods because under the hood, axis('off') calls set_axis_off(), which in turns does axison=False, so ultimately, they are the same.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文