如何通过Python中的GUI控制matplotlib图像
时间: 2023-11-27 07:54:01 浏览: 68
可以使用Python中的Tkinter模块或PyQt模块来创建GUI,然后使用matplotlib模块来创建图像并将其嵌入到GUI中。
以下是一个Tkinter的例子:
```python
import tkinter as tk
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
class MyApp:
def __init__(self, parent):
self.parent = parent
self.figure = Figure(figsize=(5, 4), dpi=100)
self.ax = self.figure.add_subplot(111)
self.canvas = FigureCanvasTkAgg(self.figure, master=self.parent)
self.canvas.get_tk_widget().pack(side='top', fill='both', expand=1)
self.toolbar = NavigationToolbar2Tk(self.canvas, self.parent)
self.toolbar.update()
self.canvas.get_tk_widget().pack(side='top', fill='both', expand=1)
self.button = tk.Button(self.parent, text='Plot', command=self.plot)
self.button.pack(side='bottom')
def plot(self):
x = np.linspace(0, 10, 100)
y = np.sin(x)
self.ax.clear()
self.ax.plot(x, y)
self.canvas.draw()
root = tk.Tk()
app = MyApp(root)
root.mainloop()
```
这个例子创建了一个GUI窗口,其中包含一个按钮和一个matplotlib图像。单击按钮会在图像中绘制一个sin曲线。
阅读全文
相关推荐


















