在子图中关闭轴 [英] Turn off axes in subplots

查看:180
本文介绍了在子图中关闭轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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

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

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

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

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

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

plt.show()

然而,由于轴上的值,结果非常难看:

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

如何同时关闭所有子图的轴值?

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

推荐答案

您可以按照Veedrac评论中的建议关闭轴(链接到这里)有一个小修改。

You can turn the axes off by following the advice in Veedrac's comment (linking to here) with one small modification.

而不是使用 plt.axis('off') 你应该使用 ax.axis('off') 其中 ax 是一个 matplotlib.axes 对象。要为您的代码执行此操作,您只需为每个子图添加 axarr [0,0] .axis('off')等等。

Rather than using plt.axis('off') you should use ax.axis('off') where ax is a matplotlib.axes object. To do this for your code you simple need to add axarr[0,0].axis('off') and so on for each of your subplots.

下面的代码显示了结果(我删除了 prune_matrix 部分,因为我无法访问该功能,将来请提交完整的工作代码。

The code below shows the result (I've removed the prune_matrix part because I don't have access to that function, in the future please submit fully working code.)

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

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

f, axarr = plt.subplots(2, 2)
axarr[0,0].imshow(img, cmap = cm.Greys_r)
axarr[0,0].set_title("Rank = 512")
axarr[0,0].axis('off')

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

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

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

plt.show()

注意:要仅关闭x或y轴,您可以使用 set_visible()例如:

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

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

这篇关于在子图中关闭轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆