Custom made plots in Python with PySimpleGUI
First question you might have is why ? After all we have perfectly good plotting libraries like PyPlot and others…
Answer: Because sometimes you need to make something not in stock at your charting library of choice, other times you might simply want to avoid integrating libraries or have an easier time without them, and then of course you might just be curious about the do it yourself approach.
⚠️ The logic here should work on any canvas element from your GUI library ( heck, you can also port the concepts to your language of choice ), I am using PySimpleGUI because it is beginner friendly and relatively simple to use, but your mileage may vary.
Ok, so what do we need ? At a minimum a plot or chart should have some elements like a GUI Window, canvas, a grid, ticks and the all important data…
So making functions for each one of these elements seems a reasonable start.
Should give you:
There’s a ton of functionality missing ( compared to a plotting library ), and that is ok, you are not trying to remake a library but rather one or a few specific cases so you can move on with you life ( and project ) !
Of course you might not want a point cloud plot, so here are a couple more popular plots that should help you get started:
The only real difference here ( Besides the slightly different data) is how the data points are added to make a line, it could also use some refactoring ( see later examples ).I am using enumerate as a loop to cycle through the data points because we can use the index to loop through the axes, but you can use other methods that better suit your data structures.
The structure/methods are still mostly unchanged, the data is slightly different as well as the drawTicks() and drawPlot() methods, the main point here is that you can design and implement whatever plot style you want by placing individual elements on top of the graph/canvas.
So what’s next ? Well the advantages of using the native canvas from your GUI to draw plots means that you can write complex/novel behavior into your plots and programs without much fuzz, I have no idea what awesome thing you are coding, so I’ll just give you 2 basic/starter cases (Updates and Animations)
The first one is simply adding a button to update the plots data…
Some considerations: By necessity the previous scripts need to become slightly more complex, for starters there is a PysimpleGUI loop that catches button events, a method for making synthetic data and a global variable to store the coordinates, to update the plot we then erase the previous graph and redraw the plot.
The benefits here are better performance and if you like to keep dependencies at a minimum, simpler code. I should also mention the alternative: Integrating PyPlot and PySimpleGUI.
To keep things simple ( Animation can be a deep subject ) I am just going to modify the previous example and continuously update the plot…
Should give you:
If you are plotting data and are having a rough time with popular libraries like PyPlot, you might want to go the DIY route and make your own plots, it’s really not that difficult if you keep a structured approach and keep things simple at the start, the benefits can be multiple ( performance, simplicity, less dependencies etc, etc. ) and if nothing else you will gain some experience by building a plot from scratch.
I hope this few examples help you get started.
Thanks for reading !