All images by author.

Custom made plots in Python with PySimpleGUI

For the DIY coder.

Keno Leon
Keno Leon
May 27 · 4 min read

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…

Just like a Sandwich 🥪 add or remove layers to taste.

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 most used plot, or is it the next one…
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:

The performance gains are worth repeating, you can increase the FPS ( reduce the timeout ) and this will still run smoothly.

Conclusion

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 !

Geek Culture

Proud to geek out. Follow to join our +500K monthly readers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store