简体   繁体   English

在子图中关闭轴

[英]Turn off axes in 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")

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? 如何同时关闭所有子图的轴值?

You can turn the axes off by following the advice in Veedrac's comment (linking to here ) with one small modification. 您可以按照Veedrac评论中的建议(链接到此处 )通过一个小修改来关闭轴。

Rather than using plt.axis('off') you should use ax.axis('off') where ax is a matplotlib.axes object. 您应该使用ax.axis('off')而不是使用plt.axis('off') ax.axis('off') ,其中axmatplotlib.axes对象。 To do this for your code you simple need to add axarr[0,0].axis('off') and so on for each of your subplots. 要为您的代码执行此操作,您只需为每个子图添加axarr[0,0].axis('off')等等。

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. ) 下面的代码显示了结果(我删除了prune_matrix部分,因为我无法访问该函数, 将来请提交完整的代码。

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()

Stewie的例子

Note: To turn off only the x or y axis you can use set_visible() eg: 注意:要仅关闭x或y轴,可以使用set_visible()例如:

axarr[0,0].xaxis.set_visible(False) # Hide only x axis
import matplotlib.pyplot as plt

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


To turn off axes for all subplots, do either: 要关闭所有子图的轴,请执行以下任一操作:

[axi.set_axis_off() for axi in ax.ravel()]

or 要么

map(lambda axi: axi.set_axis_off(), ax.ravel())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM