python - 关闭子图中的轴

标签 python image matplotlib

我有以下代码:

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

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

enter image description here

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

最佳答案

您可以按照 Veedrac 评论中的建议(链接到 here )进行一些小的修改来关闭轴。

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

下面的代码显示了结果(我删除了 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 example

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

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

关于python - 关闭子图中的轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25862026/

相关文章:

python - 存储这些向量但在 Python 中使用哪种数据结构

python - 使用极轴在 matplotlib 中进行四重显示

javascript - 如何允许用户将图像放在 html 中的另一个图像之上?

python - 视频从本地驱动器上传到 Facebook

Python PIL 轮廓图像增加厚度

python - Python请求抛出SSLError

image - 如何找到错误图像?

c# - 使用图像控件显示服务器上传的图像

matplotlib - 我可以使用 Matplotlib 和 Jupyter 加载 Google 字体吗?

python - 如何在Django模型中同步更新实例?