python - 如何在不关闭图形的情况下清除所有子图?
问题描述
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
N = 50
fig = plt.figure()
ax = fig.add_subplot(1, 2, 1, projection='3d')
ax.set_title('Cartesian Plot')
# im = fig.add_subplot(1, 2, 2)
# im.set_title('Image')
# im.axis('off')
plt.ion()
plt.show()
for p in range(10):
Z = np.random.randint(255, size=(N, N, 3))
A, B, C = Z[:, 0], Z[:, 1], Z[:, 2]
ax.scatter(A, B, C, c='r', marker='.')
# im.imshow(Z)
plt.draw()
plt.pause(1)
plt.cla()
plt.ioff()
plt.close()
我正在尝试绘制图像,并且在修改后一次又一次地绘制笛卡尔图,所以我设置了这个示例。如果您注释掉图像部分,则此代码有效,然后正确清除笛卡尔图。但是,如果您添加图像子图(取消注释im
子图)而不是清除和重绘所有子图,它们会相互绘制,这对于笛卡尔空间中的图来说是一个问题。
有人可以帮助我吗?在循环的每次迭代之后,我希望为下一次迭代清除和重绘两个子图,依此类推。
对于我所做的图像,我还希望不显示标记轴的数字,im.axis('off')
但这仅适用于第一次迭代,并且设置为默认值。
解决方案
尝试使用:
subplot.cla() # this clears the data but not the axes
subplot.clf() # this clears the data and the axes
所以在你的情况下,它将是
ax.cla()
或者
ax.clf()
推荐阅读
- java - spring cloud gateway拦截所有传入和传出请求
- python - django 中的 Urlpatterns 没有重定向(为什么没有显示响应?)
- angular - 对象可能是未定义的角度单元测试
- go - Go os args 将“.go”视为文件路径而不是字符串
- node.js - Knex.js 迁移在 Github Action 中不起作用
- php - 我想检查数组中是否存在值并打印一个随机值,但它给出了未定义的索引错误
- dart - 具有默认值的 Dart 类可选构造函数参数导致错误
- docker - 我应该为所有环境的每个微服务映像保留一个 docker 注册表吗?
- c++ - 如何用 for 循环替换这个 memcpy?
- typescript - 带有类型保护的通用虚假过滤器未正确缩小类型