Created
April 2, 2024 21:36
-
-
Save vigilantPotato/8a1390f6a37c54fcf1e01ab9962d1a54 to your computer and use it in GitHub Desktop.
How to control matplotlib graph from tkinter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ctypes | |
import numpy as np | |
import tkinter | |
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk | |
from matplotlib.figure import Figure | |
#function slidebar is moved | |
def update_graph(value): | |
d = int(value) | |
y = np.sin(d * x) | |
line.set_ydata(y) | |
canvas.draw() | |
if __name__ == "__main__": | |
ctypes.windll.shcore.SetProcessDpiAwareness(1) | |
root = tkinter.Tk() | |
root.title("matplotlib 埋め込み") | |
#initial Graph data | |
x = np.linspace(0, 2*np.pi, 400) | |
y = np.sin(x) | |
#Graph object | |
fig = Figure(figsize=(3, 3), dpi=100) #Figure | |
ax = fig.add_subplot(1, 1, 1) #Axes | |
line, = ax.plot(x, y) #2DLine | |
#set graph to tkinter | |
canvas = FigureCanvasTkAgg(fig, root) | |
toolbar = NavigationToolbar2Tk(canvas, root) | |
canvas.get_tk_widget().pack() #show graph | |
#scale | |
s = tkinter.Scale( | |
root, | |
width = 15, | |
orient = "horizontal", | |
command = update_graph, | |
) | |
s.pack() | |
root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment