Created
April 18, 2024 21:45
-
-
Save vigilantPotato/7b4e82252ad038427cceac1cd8f09cfb to your computer and use it in GitHub Desktop.
How to create bar graph by matplotlib
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 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.arange(5) | |
y = [1, 5, 2, 9, 10] | |
#create graph objects | |
fig, ax = plt.subplots(ncols=2) | |
ax[0].bar(x, y) #vertical | |
ax[1].barh(x, y) #horizontal | |
#set title | |
fig.suptitle("bar graph") | |
ax[0].set_title("vertical") | |
ax[1].set_title("horizontal") | |
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