简体   繁体   中英

Removing a plotted point in scatter plot - matplotlib

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.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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