From 7b8c458b632ae6e2a0564e9142d083f4fffd9abf Mon Sep 17 00:00:00 2001 From: mputzi Date: Sat, 16 Apr 2022 11:32:52 +0200 Subject: [PATCH] Example for embedding matplotlib in tkinter widgets --- tk_canvas_matplotlib/matplotlibtest.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 tk_canvas_matplotlib/matplotlibtest.py diff --git a/tk_canvas_matplotlib/matplotlibtest.py b/tk_canvas_matplotlib/matplotlibtest.py new file mode 100644 index 0000000..557aa14 --- /dev/null +++ b/tk_canvas_matplotlib/matplotlibtest.py @@ -0,0 +1,23 @@ +from tkinter import * +from tkinter.ttk import * + +import matplotlib +matplotlib.use("TkAgg") +from matplotlib.figure import Figure +from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg + +root = Tk() + +figure = Figure(figsize=(5, 4), dpi=100) +plot = figure.add_subplot(1, 1, 1) + +plot.plot(0.5, 0.3, color="red", marker="o", linestyle="") + +x = [ 0.1, 0.2, 0.3 ] +y = [ -0.1, -0.2, -0.3 ] +plot.plot(x, y, color="blue", marker="x", linestyle="") + +canvas = FigureCanvasTkAgg(figure, root) +canvas.get_tk_widget().grid(row=0, column=0) + +root.mainloop()