I'm using matplotlib with python and tkinter. My graph plots 3 lines. Here is the related code:
fig = plt.Figure(figsize=(8, 6), dpi=92)
canvas = FigureCanvasTkAgg(fig, master=root) # A tk.DrawingArea.
canvas.get_tk_widget().grid(row=2, column=3, columnspan=2, rowspan=2)
plot1 = fig.add_subplot(111)
plot1.set_ylim(40, 210)
plot2 = plot1.twinx()
sysline = plot1.plot(listDateTime, listSystolic, color='orange', label = "Systolic", linewidth=2, linestyle='solid')
dialine = plot1.plot(listDateTime, listDiastolic, color='red', label = "Diastolic", linewidth=2, linestyle='solid')
weightline = plot2.plot(listDateTime, listWeight, color='blue', label = "Weight", linewidth=2, linestyle='dotted')
plot2.set_ylim(160, 220)
for label in plot1.xaxis.get_ticklabels():
label.set_rotation(-90)
plot1.xaxis.set_major_formatter(ticker.FuncFormatter(getDateString))
plot1.set_xlabel("Test Date")
plot1.set_ylabel("Pressure (mmHg)")
plot2.set_ylabel("Weight (lbs)")
plot1.set_title(graphTitle)
plot1.legend(loc='upper left')
plot2.legend(loc='upper right')
plot1.grid(True)
# plot1.set_autoscale_on(True)
# plot2.set_autoscale_on(True)
plt.tight_layout()
canvas.draw()
When I interactively load in an updated dataset, the new lines overlay the old ones, and I would prefer to delete the old lines and replot the data.
Because it's on a subplot, this approach doesn't work:
line1 = plot1.pop(0) line1.remove() line2 = plot2.pop(0) line2.remove()
I've tried getting handles for the lines and using .remove(), which doesn't raise an error. But it doesn't have any effect, either.
Does .remove() not happen until the graph is redrawn? What am I missing here?