简体   繁体   English

如何使用Python删除地块?

[英]How can i remove a plot with Python?

I have some data that I plotted with Python but now I want to erase the plots but not the figure itself. 我有一些用Python绘制的数据,但是现在我想删除图形,而不是图形本身。

I have some thing like this : 我有这样的事情:

import numpy as np
import pylab as plt

a = np.array([1,2,3,4,5,6,7,8,9,10])
b = np.array([1,2,3,4,5,6,7,8,9,10])
c = plt.plot(a,b,'r.')

So to clear this I tried this : 因此,为清除此问题,我尝试了以下操作:

a = np.array([])
b = np.array([])
c = plt.plot(a,b,'r.')

but it does not work. 但它不起作用。 What is the best way to accomplish this? 做到这一点的最佳方法是什么?

You can use the remove method of the returned plot object. 您可以使用返回的绘图对象的remove方法。 This is true of any plot object that inherits from Artist . Artist继承的任何绘图对象都是如此。

c = plt.plot(a,b,'r.')

for handle in c:
    handle.remove()

From here: 从这里:

When to use cla(), clf() or close() for clearing a plot in matplotlib? 何时使用cla(),clf()或close()在matplotlib中清除图?

plt.cla() clears an axis, ie the currently active axis in the current figure. plt.cla()清除轴,即当前图中的当前活动轴。 It leaves the other axes untouched. 它使其他轴保持不变。

plt.clf() clears the entire current figure with all its axes, but leaves the window opened, such that it may be reused for other plots. plt.clf()使用其所有轴清除整个当前图形,但使窗口保持打开状态,以便可以将其重新用于其他绘图。

plt.close() closes a window, which will be the current window, if not specified otherwise. plt.close()关闭一个窗口,如果没有另外指定,它将是当前窗口。

Also if you prefer doing it line by line, you can remove them like this even if you've lost original references: 另外,如果您更喜欢逐行进行操作,则即使丢失了原始引用,也可以像这样删除它们:

for l in ax.get_lines():
    xval = l.get_xdata()[0]
    if (xval == my_criteria):
        l.remove()

or for all, simply: 或简单地说:

for l in ax.get_lines():
            l.remove()

likewise you can do the same indexing by y values. 同样,您可以通过y值进行相同的索引编制。

To have axes with the same values of your a, b arrays, you can do: 要使轴具有与a,b数组相同的值,可以执行以下操作:

import matplotlib.pyplot as plt
plt.clf() # To clear the figure.
plt.axis([1,10,1,10])

在此处输入图片说明

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

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