Issue: In the book „Listing 250. plotting/matplotlib_4.py" I do not understand how the data from the line:
self._plot_ref.set_ydata(self.ydata)
gets into the canvas object.
My understanding is (maybe I am wrong):
In the MainWindow class getting the lists xdata
and ydata
created. The line:
plot_refs = self.canvas.axes.plot(self.xdata, self.ydata, "r")
copies the current data content into the canvas object once for the first time the canvas is drawn. From the second time, the “else” statement runs.
Question
How does the line:
self._plot_ref.set_ydata(self.ydata)
gets the data into the canvas object?
Here the complete function update_plot
def update_plot(self):
# Drop off the first y element, append a new one.
self.ydata = self.ydata[1:] + [random.randint(0, 10)]
# Note: we no longer need to clear the axis.
if self._plot_ref is None:
# First time we have no plot reference, so do a normal plot.
# .plot returns a list of line <reference>s, as we're
# only getting one we can take the first element.
plot_refs = self.canvas.axes.plot(
self.xdata, self.ydata, "r"
)
self._plot_ref = plot_refs[0]
else:
# We have a reference, we can use it to update the data for that line.
self._plot_ref.set_ydata(self.ydata)
# Trigger the canvas to update and redraw.
self.canvas.draw()