清除 Matplotlib 中的子图 [英] Clearing a subplot in Matplotlib

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

问题描述

我在一个图fig1中有许多子图,通过

创建

ax = fig1.add_subplot(221)

然后我通过

在每个子图中绘制内容

  im = ax.plot(x,y)

并通过

添加一些轴标签

ax.set_xlabel('xlabel')

然后我想完全清除特定的子图,如

I have a number of subplots in a figure fig1, created via

ax = fig1.add_subplot(221)

I then plot stuff in each of the subplots via

im=ax.plot(x,y)

and add some axis labels via

ax.set_xlabel('xlabel')

I would then like to clear a specific subplot completely, as described in When to use cla(), clf() or close() for clearing a plot in matplotlib?. However the problem is that ax.cla()and ax.clear() seem to only clear the data from the plot, without removing the axes, axis tick labels etc. On the other hand plt.clf() clears the entire figure. Is there something in between? A clf-like command that clears everything in a subplot, including axis labels? Or have I simply used the commands in a wrong way?

解决方案

  • ax.clear() clears the axes. That is, it removes all settings and data from the axes such that you are left with an axes, just as it had been just created.

  • ax.axis("off") turns the axes off, such that all axes spines and ticklabels are hidden.

  • ax.set_visible(False) turns the complete axes invisible, including the data that is in it.

  • ax.remove() removes the axes from the figure.

Complete example:

import matplotlib.pyplot as plt

fig,axes = plt.subplots(2,3)
for ax in axes.flat:
    ax.plot([2,3,1])

axes[0,1].clear()
axes[1,0].axis("off")
axes[1,1].set_visible(False)
axes[0,2].remove()

plt.show()

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

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