Mikhail Valiev
Mikhail Valiev

Reputation: 113

How do you remove a point from matplotlib plot?

Lets say you have a plot in matplotlib, something like that:

figure = Figure()
figureCanvas = FigureCanvas(figure)
axes = figure.add_subplot(111)
axes.plot([1, 2, 3], [2, 3, 1], linestyle = "None", marker = "o", color = '#1f77b4', markersize = 3)

This would give you a plot with 3 points. How do I remove a specific point from plot, without redrawing the whole thing again?

Upvotes: 0

Views: 4364

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339092

First of all, you need to redraw at least the plot (the Line2D object), otherwise there will be no change in the plot.

Without knowing the purpose of not redrawing, it's hard to judge on an acceptable solution. However, usually you would just redraw the whole canvas. To set new data, the Line2D.set_data() method can be used as shown in the following. You may press the number key (0,1,2) of the point to remove in the plot.

import matplotlib.pyplot as plt

x = [1, 2, 3]
y = [2, 3, 1]

fig, ax = plt.subplots()
line, = ax.plot(x, y, ls="None", marker="o", color='#1f77b4', ms=10)

def remove_point(event):
    try:
        key = int(event.key)
        xvals = x[:]
        xvals.pop(key)
        yvals = y[:]
        yvals.pop(key)
        line.set_data(xvals,yvals)
        fig.canvas.draw_idle()
    except:
        pass

fig.canvas.mpl_connect('key_press_event', remove_point)
ax.set_title("Press number of point to remove")
plt.show()

Upvotes: 1

Related Questions