Created
April 3, 2024 21:55
-
-
Save vigilantPotato/770b75693377f4bc80d5e158185a1bb6 to your computer and use it in GitHub Desktop.
How to set matplotlib graph into tkinter window (by subplots)
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 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