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/770b75693377f4bc80d5e158185a1bb6 to your computer and use it in GitHub Desktop.
Save vigilantPotato/770b75693377f4bc80d5e158185a1bb6 to your computer and use it in GitHub Desktop.
How to set matplotlib graph into tkinter window (by subplots)
import ctypes
import matplotlib.pyplot as plt
import numpy as np
import tkinter
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
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, ax = plt.subplots() #subplots
ax.plot(x, y) #2DLine
plt.close() #!!close!!
#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