如何在不关闭图形的情况下清除所有子图?

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)
# 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],2]
    ax.scatter(A,C,c='r',marker='.')
    # im.imshow(Z)

    plt.draw()
    plt.pause(1)
    plt.cla()

plt.ioff()
plt.close()

我试图绘制图像,并且在修改后一次又一次是笛卡尔图,因此我设置了这个示例。如果注释掉图像部分,则此代码有效,然后正确清除了笛卡尔图。但是,如果您添加图像子图(取消对im子图的注释)而不是清除并重新绘制所有子图,则它们会相互绘制,这在笛卡尔空间中是一个问题。

有人可以帮我吗?在循环的每次迭代之后,我都希望清除两个子图并为下一次迭代重新绘制,以此类推。

对于我为im.axis('off')做的图像,我也希望标注轴的数字不显示,但仅适用于第一次迭代,并且将其设置为默认值。

bingning128 回答:如何在不关闭图形的情况下清除所有子图?

尝试使用:

subplot.cla()  # this clears the data but not the axes
subplot.clf()  #  this clears the data and the axes

因此,您的情况将是

ax.cla()

ax.clf()
本文链接:https://www.f2er.com/3106340.html

大家都在问