Animated Line Graph Using PySimpleGUI

Posted by


Introduction

PySimpleGUI is a Python library that simplifies the process of creating graphical user interfaces (GUIs) for desktop applications. One of the features it offers is the ability to create animated line graphs, which can be a powerful tool for visualizing data in real-time. In this tutorial, we will walk you through the process of creating an animated line graph using PySimpleGUI.

Installation

Before we begin, you’ll need to install PySimpleGUI. You can do this by running the following command in your terminal:

pip install PySimpleGUI

Creating the Animated Line Graph

To create an animated line graph using PySimpleGUI, we first need to import the necessary modules and set up the layout of our GUI. Here’s an example code snippet that does this:

import PySimpleGUI as sg
import matplotlib.pyplot as plt
import matplotlib.backends.backend_tkagg as tkagg
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import numpy as np

layout = [
    [sg.Graph((800, 600), (-10, -10), (10, 10), background_color='white', key='graph')],
    [sg.Button('Start'), sg.Button('Stop')]
]

window = sg.Window('Animated Line Graph', layout)
graph = window['graph']

fig, ax = plt.subplots()
x = np.linspace(0, 10, 100)
y = np.sin(x)
line, = ax.plot(x, y)

canvas = FigureCanvasTkAgg(fig, graph.Widget)
canvas.get_tk_widget().pack()

running = False

while True:
    event, values = window.read(timeout=10)
    if event == sg.WIN_CLOSED:
        break
    if event == 'Start':
        running = True
    if event == 'Stop':
        running = False

    if running:
        y = np.roll(y, -1)
        y[-1] = np.sin(x[-1] + 0.1)
        line.set_ydata(y)
        canvas.draw()
        graph.draw_figure(canvas.figure)

Explanation of the Code

In this code snippet, we import the necessary modules, create a window with a graph element, and set up the layout. We then create a figure and axis for the line graph, create a line object to draw the line on the graph, and create a canvas to display the graph in the window.

We then enter a loop where we wait for user input. If the user clicks the ‘Start’ button, we set the running flag to True, which starts the animation. If the user clicks the ‘Stop’ button, we set the running flag to False, which stops the animation.

Inside the loop, if the animation is running, we update the y values of the line graph by shifting them to the left and adding a new value at the right. We then update the line on the graph using the set_ydata() method, draw the canvas, and update the graph in the window using the draw_figure() method.

Conclusion

In this tutorial, we walked you through the process of creating an animated line graph using PySimpleGUI. By following the steps outlined in this tutorial, you should be able to create your own animated line graphs and visualize data in real-time. Feel free to modify the code and experiment with different animations and data to create custom visualizations.

0 0 votes
Article Rating

Leave a Reply

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@keithlohmeyer
3 hours ago

Nice description of moving charts. One thing you mentioned you based this on another project. It must have been done in early PSG as some of your commands are deprecated although still supported (for now).
sg.change_look_and_feel is replaced by sg.theme
sg.Button('Exit') is replaced by sg.Exit()
and you probably should include sg.WIN_CLOSED in your break event.
Thanks for another great vid!

@k_himank
3 hours ago

Thank you for your tutorials man. Pretty underrated.

2
0
Would love your thoughts, please comment.x
()
x