How it works...

Here is the explanation for the preceding code:

  • fig = Figure(figsize=(6, 5), dpi=100) defines and instantiates the figure object. Figure() is imported from the matplotlib.figure class. In the two previous recipes, we used plt.figure() for this purpose!
  • window=tk.Tk() invokes the Tkinter GUI application through its main window widget.
  • window.wm_title("Embedding in Tk") sets the title for the window, which will appear on the top of the window.
  • canvas = FigureCanvasTkAgg(fig, master=window) defines the canvas (the playground for the figure, such as the paper on which a graph is plotted), and attaches the figure to this canvas:
    • master=window specifies that the canvas is a child of the Tkinter window object.
    • toolbar = NavigationToolbar2Tk(canvas, ...

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.