I'm making an interface, where I have have some "clear this" and "clear that" buttons to remove various elements of a plot.
The problem with matplotlib is that I have to know the exact order the objects were plotted in to remove the correct one with ax.lines.pop(). For example, a plot could contain raw data, then a smoothed version, and then a fit on top, but depending on the order these were called, ax.lines.pop(2) would remove either the blue or the red line.
But how can I consistently remove e.g. the red line in a multi-layered scenario?
import matplotlib.pyplot as plt
import numpy as np
from scipy.signal import medfilt
a = np.random.normal(0, 1, 100)
b = medfilt(a, 17)
fig1, axes = plt.subplots(ncols = 2)
ax1, ax2 = axes
ax1.set_title("Figure 1")
ax1.plot(a, color = "darkgrey") # 0
ax1.plot(b, color = "firebrick") # 1
ax1.axhline(0.5, color = "blue") # 2
ax1.lines.pop(2)
ax2.set_title("Figure 2")
ax2.plot(a, color = "darkgrey") # 0
ax2.axhline(0.5, color = "blue") # 2
ax2.plot(b, color = "firebrick") # 1
ax2.lines.pop(2)
plt.show()
