Skip to content

Instantly share code, notes, and snippets.

@MikeTheWatchGuy
Last active December 21, 2018 19:42
Show Gist options
  • Save MikeTheWatchGuy/fd79baad627ae879bb74437427ad4ff0 to your computer and use it in GitHub Desktop.
Save MikeTheWatchGuy/fd79baad627ae879bb74437427ad4ff0 to your computer and use it in GitHub Desktop.
import PySimpleGUI as sg
import random
BAR_WIDTH = 50
BAR_SPACING = 75
EDGE_OFFSET = 3
GRAPH_SIZE = (500,500)
DATA_SIZE = (500,500)
graph = sg.Graph(GRAPH_SIZE, (0, 0), DATA_SIZE)
layout = [[sg.Text('Bar graphs using PySimpleGUI')],
[graph],
[sg.Button('OK')]]
window = sg.Window('Window Title').Layout(layout)
while True:
event, values = window.Read()
if event is None:
break
graph.Erase()
for i in range(7):
graph_value = random.randint(0, 400)
graph.DrawRectangle(top_left=(i * BAR_SPACING + EDGE_OFFSET, graph_value),
bottom_right=(i * BAR_SPACING + EDGE_OFFSET + BAR_WIDTH, 0), fill_color='blue')
graph.DrawText(text=graph_value, location=(i*BAR_SPACING+EDGE_OFFSET+25, graph_value+10))
@MikeTheWatchGuy
Copy link
Author

MikeTheWatchGuy commented Dec 17, 2018

The coordinate system is (0,0) is the bottom left corner. (500,500) is the top right. This is identical to the positive portion of a 4-quadrant graph. You can just as easy define a traditional graph with 0,0 being in the center of the diagram. For that you use (-250,-250) for bottom left and (250,250) for top right.

Here's the image produced when using a 500x500 size.

image

@MikeTheWatchGuy
Copy link
Author

MikeTheWatchGuy commented Dec 17, 2018

This 100x100 size was generated by changing only the line of code defining the pixel size of the graph (GRAPH_SIZE)

image

@eagleEggs
Copy link

Thanks. I'm definitely replacing MPL with this. It's fast flexible, customizable and doesn't require an additional 50MB for a simple bar graph :)

image

Working on some things. Going to push the SQl data into it, and customize some more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment