To embed Matplotlib plots in GUIs like Tkinter or PyQt, you can use the following methods:
For Tkinter:
1. Create a `FigureCanvasTkAgg` object with your Matplotlib figure.
2. Pack the canvas into your Tkinter window.
Example:
“`python
from tkinter import Tk
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
root = Tk()
fig = Figure()
ax = fig.add_subplot(111)
ax.plot([1, 2, 3], [1, 4, 9])
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().pack()
root.mainloop()
“`
For PyQt:
1. Create a `FigureCanvasQTAgg` object with your Matplotlib figure.
2. Add the canvas to your PyQt layout.
Example:
“`python
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget