开发者

Erase previously drawn content from a pyplot drawing

开发者 https://www.devze.com 2023-03-30 15:53 出处:网络
Using pyplot, I\'ve created a figure and plotted a number of randomly scattered (x,y) points. I then want to connect some subset of these points with line segments. After the user 开发者_开发技巧press

Using pyplot, I've created a figure and plotted a number of randomly scattered (x,y) points. I then want to connect some subset of these points with line segments. After the user 开发者_开发技巧presses a key, my program needs to then erase the previously drawn set of line segments (leaving the original points in place) and then draw new segments between another set of points.

My problem is that I don't know how to erase the previous line segments. I tried re-plotting them in the background color (which is white) but this leaves a residue of non-white pixels where the line was. I can't find any examples in the on-line documentation that does this. Is it possible and, if so, how?


Scipy has a tutorial for deleting a line from an axes. I use this in the following example, which I think emulates what you are trying to do:

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(10)
np.random.seed(101)
y = np.random.rand(10)

small = [i for i in range(len(x)) if y[i] < .5 ]
big = [i for i in range(len(x)) if y[i] > .5 ]

fig = plt.figure()
ax = fig.add_subplot(111)

ax.scatter(x,y)
# user wants to plot lines connecting big values (>.5)
ax.plot(x[big],y[big])

#plt.show()
# now user wants to delete the first (and only) line
del ax.lines[0]

# so that they can plot a line only showing small values
ax.plot(x[small],y[small])

plt.show()


You'll need to clear the plot and redraw your points, unfortunatly. The command is clf().

I just realized I'm sort of wrong. Look up the artists concept, it allows you to define certain drawn objects as static, and others as dynamic. I've used it for animations, but it should be applicable here also. Here's a tutorial.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号