Skip to content

Instantly share code, notes, and snippets.

@vigilantPotato
Created April 16, 2024 21:21
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/4fbf77a857a19bc8a08e168c43411713 to your computer and use it in GitHub Desktop.
Save vigilantPotato/4fbf77a857a19bc8a08e168c43411713 to your computer and use it in GitHub Desktop.
How to use matplotlib-subplots
import ctypes
import tkinter
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
if __name__ == "__main__":
ctypes.windll.shcore.SetProcessDpiAwareness(1)
root = tkinter.Tk()
#graph data
x = np.linspace(0, 2*np.pi, 400)
y = np.sin(x**2)
#create graph objects
fig, ax = plt.subplots(ncols=2, nrows=2)
ax[0, 0].plot(x,y)
ax[1, 0].scatter(x,y)
ax[0, 1].bar(x,y)
ax[1, 1].fill(x,y)
#set title
fig.suptitle("fig object")
ax[0, 0].set_title("ax[0, 0] plot")
ax[1, 0].set_title("ax[1, 0] scater")
ax[0, 1].set_title("ax[0, 1] bar")
ax[1, 1].set_title("ax[1, 1] fill")
plt.tight_layout()
#set graph to tkinter window
canvas = FigureCanvasTkAgg(fig, root)
toolbar = NavigationToolbar2Tk(canvas, root)
canvas.get_tk_widget().pack() #show graph
plt.close()
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment