Álvaro
Álvaro

Reputation: 1307

Removing a certain part of a figure in matplotlib

I am trying to do the following: plot two different datasets, ask the user which one is better, remove the bad one from the figure, and save the good plot. However, I can't find a way to do it with matplotlib. I found the cla() command, but even like that it keeps on deleting the whole figure... can someone give me a hand with this?

Here is how I am doing it.

fig=plt.figure()
ax=fig.add_subplot(111)
plot(something)
ax2=fig.add_subplot(111)
plot(other thing)
cla()

Thanks!

Upvotes: 1

Views: 3066

Answers (1)

unutbu
unutbu

Reputation: 879103

This plots two curves. Clicking on a curve deletes it.

import matplotlib.pyplot as plt
import numpy as np
sin = np.sin
cos = np.cos
pi = np.pi

def delete(event):
    artist = event.artist
    artist.remove()
    event.canvas.draw()

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

x = np.linspace(0, 2*pi, 50)
y = 2*sin(x)
z = 2*cos(x)
line1, = ax.plot(x,y)
line2, = ax.plot(x,z)
for artist in [line1, line2]:
    artist.set_picker(5)
fig.canvas.mpl_connect('pick_event', delete)

plt.show()

Upvotes: 3

Related Questions