How it works...

Here is the explanation for the preceding code:

  • The first four steps are already familiar to you.
  • window = fig.canvas.manager.window gets access to Tkinter's application through its window.
  • def update(): is the callback function for the tk.Scale widget:
    • n=n_slider.get() gets the current value of the slider, when the mouse was dragged and released, which in this case is 7.
    • ax.clear() erases the previous plot on the axes.
    • ax.plot(theta, 5*np.cos(n*theta)) plots the new graph with a current value of n.
    • fig.canvas.draw() refreshes the figure.
  • n_slider = tk.Scale() defines the slider widget with the following arguments:
    • master=window specifies that the slider widget should be created as a child of the main window.
    • variable=tk.IntVar() ...

Get Matplotlib 3.0 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.