-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemoPrompt10.b_ChatGPT+Matplotlib.py
39 lines (32 loc) · 1.1 KB
/
demoPrompt10.b_ChatGPT+Matplotlib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import tkinter as tk
from tkinter import ttk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import numpy as np
def plot_matplotlib():
# Create sample data
x = np.linspace(0, 2 * np.pi, 100)
y = np.sin(x)
# Create a Matplotlib figure
fig = Figure(figsize=(5, 4), dpi=100)
ax = fig.add_subplot(1, 1, 1)
ax.plot(x, y, label='Sine Wave')
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_title('Matplotlib Plot')
ax.legend()
# Embed the Matplotlib figure in Tkinter
canvas = FigureCanvasTkAgg(fig, master=frame)
canvas.draw()
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
# Create the main Tkinter window
root = tk.Tk()
root.title("Tkinter with Matplotlib")
# Create a frame within the window
frame = ttk.Frame(root)
frame.pack(expand=True, fill=tk.BOTH)
# Create a button to trigger Matplotlib plot
button = ttk.Button(frame, text="Plot Matplotlib", command=plot_matplotlib)
button.pack(side=tk.BOTTOM)
# Start the Tkinter event loop
root.mainloop()