Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vigilantPotato/8a1390f6a37c54fcf1e01ab9962d1a54 to your computer and use it in GitHub Desktop.
Save vigilantPotato/8a1390f6a37c54fcf1e01ab9962d1a54 to your computer and use it in GitHub Desktop.
How to control matplotlib graph from tkinter
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