Reputation: 72
Following sample is a simplified version on matplotlib scatterplot example provided on their website, and shows my attempt to remove a point from scatter plot
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
# the random data
x = np.random.randn(2)
y = np.random.randn(2)
fig, axScatter = plt.subplots(figsize=(5.5,5.5))
# the scatter plot:
axScatter.scatter(x, y)
axScatter.set_aspect(1.)
np.delete(x,1)
np.delete(y,1)
axScatter.scatter(x, y)
plt.draw()
plt.show()
I could not figure out a way to remove a point from scatter plot after plotting, though if i use the same method i can plot a new point.
Upvotes: 3
Views: 7721
Reputation: 284582
You need to update the data of the plotted artist.
Also numpy.delete
returns a copy of the array with the item deleted, so calling np.delete(x, 1)
doesn't modify the original x
. You'll need to use x = np.delete(x, 1)
instead, if you want to delete the second item.
As a quick example:
import numpy as np
import matplotlib.pyplot as plt
import time
x, y = np.random.random((2, 10))
fig, ax = plt.subplots()
scat = ax.scatter(x, y, s=150)
# Show the figure, then remove one point every second.
fig.show()
for _ in range(10):
time.sleep(1)
xy = np.delete(scat.get_offsets(), 0, axis=0)
scat.set_offsets(xy)
plt.draw()
Upvotes: 2