Created
April 21, 2024 21:55
-
-
Save vigilantPotato/b2e82eb92aee68b35b2d15b753332af1 to your computer and use it in GitHub Desktop.
How to create group or stack bar graph
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 = [1, 5, 2, 9, 10] | |
y_2 = [3, 1, 2, 6, 7] | |
#create graph objects | |
fig, ax = plt.subplots(ncols=2) | |
#show graph | |
width=0.3 #graph width | |
ax[0].bar(x-width/2, y_1, width=width) #shift to left | |
ax[0].bar(x+width/2, y_2, width=width) #shift to right | |
ax[1].bar(x, y_1) | |
ax[1].bar(x, y_2, bottom=y_1) #height offset | |
#set title | |
fig.suptitle("bar graph") | |
ax[0].set_title("group") | |
ax[1].set_title("stack") | |
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