Skip to content

Instantly share code, notes, and snippets.

@vigilantPotato
Created April 2, 2024 21:24
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/7aca6ea913c34bafe0a26fb7117838bc to your computer and use it in GitHub Desktop.
Save vigilantPotato/7aca6ea913c34bafe0a26fb7117838bc to your computer and use it in GitHub Desktop.
How to set matplotlib graph into tkinter window
import ctypes
import numpy as np
import tkinter
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
if __name__ == "__main__":
ctypes.windll.shcore.SetProcessDpiAwareness(1)
root = tkinter.Tk()
root.title("matplotlib 埋め込み")
#Graph data
x = np.linspace(0, 2*np.pi, 400)
y = np.sin(x)
#Graph object
fig = Figure(figsize=(5, 5), 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
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment