写一段代码实现“设置散点图等图像选择及显示按钮,选择相应图像后,点击,界面可显示对应图形“的功能
时间: 2023-12-07 07:02:41 浏览: 23
好的,以下是一个简单的示例代码,可以实现你所描述的功能:
```python
import matplotlib.pyplot as plt
from tkinter import *
# 创建 Tkinter 窗口
root = Tk()
# 设置窗口标题
root.title("图像选择")
# 创建标签
label = Label(root, text="请选择要显示的图像:")
label.pack()
# 创建按钮
button1 = Button(root, text="散点图")
button1.pack()
button2 = Button(root, text="折线图")
button2.pack()
button3 = Button(root, text="柱状图")
button3.pack()
# 创建事件处理函数
def show_scatter():
# 创建数据
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# 绘制散点图
plt.scatter(x, y)
plt.show()
def show_line():
# 创建数据
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# 绘制折线图
plt.plot(x, y)
plt.show()
def show_bar():
# 创建数据
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# 绘制柱状图
plt.bar(x, y)
plt.show()
# 绑定事件处理函数
button1.config(command=show_scatter)
button2.config(command=show_line)
button3.config(command=show_bar)
# 运行窗口
root.mainloop()
```
这个程序使用了 Matplotlib 库绘制散点图、折线图和柱状图,并使用 Tkinter 库创建了一个简单的图形用户界面。当用户点击相应的按钮时,就会显示对应的图形。