import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import tkinter as tk
# Create a GUI window
root = tk.Tk()
root.title("Multiple Plots")
root.geometry("1800x600")
# Create four subframes for displaying plots
frame1 = tk.Frame(root)
frame1.pack(side=tk.LEFT, padx=10, pady=10)
frame2 = tk.Frame(root)
frame2.pack(side=tk.LEFT, padx=10, pady=10)
# Create some data and plot it on each frame
x = [0, 1, 2, 3, 4, 5]
y1 = [0, 1, 4, 9, 16, 25]
y2 = [0, 1, 8, 27, 64, 125]
fig1, ax1 = plt.subplots()
ax1.plot(x, y1)
for i in range(len(x)):
ax1.annotate(f"({x[i]}, {y1[i]})", (x[i], y1[i]))
fig2, ax2 = plt.subplots()
ax2.plot(x, y2)
for i in range(len(x)):
ax2.annotate(f"({x[i]}, {y2[i]})", (x[i], y2[i]))
canvas1 = FigureCanvasTkAgg(fig1, master=frame1)
canvas1.draw()
canvas1.get_tk_widget().pack()
canvas2 = FigureCanvasTkAgg(fig2, master=frame2)
canvas2.draw()
canvas2.get_tk_widget().pack()
# Show the window
root.mainloop()
显示多个折线图
该代码示例展示了如何利用matplotlib库和TkinterGUI框架创建一个包含两个子帧的应用,每个子帧显示不同的数据图表。每个图表都有对应的坐标标注,提供了一种在PythonGUI中展示多个数据图形的方法。
摘要由CSDN通过智能技术生成