button1 = tk.Button(root, text="散点图", bg="light yellow", fg="dark blue", command=import_csv_data, relief="raised", borderwidth=3, activebackground="light blue", activeforeground="black") button1.pack(side="top") #创建一个框架(Frame控件),用于放置图像。 fig_container = tk.Frame(root) fig_container.pack(side="bottom", fill="x", expand=True) # 添加滚动框架 canvas = tk.Canvas(fig_container) canvas.pack(side="left", fill="both", expand=True) scrollbar = tk.Scrollbar(fig_container, orient="vertical", command=canvas.yview) scrollbar.pack(side="right", fill="y") canvas.configure(yscrollcommand=scrollbar.set) canvas.bind("<Configure>", lambda e: canvas.configure(scrollregion=canvas.bbox("all"))) # 将图像框架放入滚动框架中 frame = tk.Frame(canvas) canvas.create_window((0, 0), window=frame, anchor="nw") def show_figure(): x = df.iloc[:, 0] y = df.iloc[:, 1] if plt.fignum_exists(1): plt.clf() plt.scatter(x, y) # 将图像框架放在滚动框架中 canvas = FigureCanvasTkAgg(plt.gcf(), master=frame) canvas.draw() canvas.get_tk_widget().pack() button1.config(command=lambda: (button1.config(state="disabled"), show_figure())) 在这段代码里添加“折线图”功能
时间: 2024-03-03 10:46:54 浏览: 265
你可以通过在show_figure函数中使用plt.plot()函数来绘制折线图。以下是修改后的代码:
```python
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt
# ...
def show_figure():
x = df.iloc[:, 0]
y = df.iloc[:, 1]
if plt.fignum_exists(1):
plt.clf()
fig, ax = plt.subplots()
ax.plot(x, y)
# 将图像框架放在滚动框架中
canvas = FigureCanvasTkAgg(fig, master=frame)
canvas.draw()
canvas.get_tk_widget().pack()
button1.config(command=lambda: (button1.config(state="disabled"), show_figure()))
```
在这个例子中,我们使用plt.subplots()函数创建一个新的Figure对象和Axes对象,并使用ax.plot()函数绘制折线图。然后,我们将Figure对象传递给FigureCanvasTkAgg()函数,并使用get_tk_widget().pack()将图像框架添加到scrollable frame中。注意,你需要导入FigureCanvasTkAgg类和matplotlib.pyplot模块。
阅读全文