tkinter 显示matplotlib的图
时间: 2023-04-16 20:05:29 浏览: 124
可以使用Tkinter和Matplotlib库来显示Matplotlib的图形。具体步骤如下:
1. 导入Tkinter和Matplotlib库
```python
import tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
```
2. 创建Tkinter窗口和Matplotlib图形
```python
root = tk.Tk()
root.title("Matplotlib图形显示")
fig = Figure(figsize=(5, 4), dpi=100)
ax = fig.add_subplot(111)
ax.plot([1, 2, 3, 4, 5], [10, 5, 20, 15, 25])
```
3. 将Matplotlib图形嵌入到Tkinter窗口中
```python
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.draw()
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
```
4. 运行Tkinter窗口
```python
tk.mainloop()
```
完整代码如下:
```python
import tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
root = tk.Tk()
root.title("Matplotlib图形显示")
fig = Figure(figsize=(5, 4), dpi=100)
ax = fig.add_subplot(111)
ax.plot([1, 2, 3, 4, 5], [10, 5, 20, 15, 25])
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.draw()
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
tk.mainloop()
```
阅读全文