Skip to content

Instantly share code, notes, and snippets.

@vigilantPotato
Created April 18, 2024 21:45
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/7b4e82252ad038427cceac1cd8f09cfb to your computer and use it in GitHub Desktop.
Save vigilantPotato/7b4e82252ad038427cceac1cd8f09cfb to your computer and use it in GitHub Desktop.
How to create bar graph by matplotlib
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