© Copyright, 2025 G. Schaer.

SPDX-License-Identifier: GPL-3.0-only

Animator Example Usage

This module provides an example usage case for the animator. Here we create a figure with 2 plots, 1 line plot and 1 bar chart. On the line plot, we plot 2 lines. On the bar chart, we show 4 bars.

[1]:
# Required imports
import time
from condynsate import Animator
import numpy as np

First we create the animator. In this case, we set the frame rate to 20 fps, and tell it not to record

[2]:
# Create the animator with a frame rate of 20 fps
animator = Animator(frame_rate=20.0, record=False)

Next, we add the plots that we want to the animator. In this case, we want one line plot with two lines and one bar chart with 4 bars.

[3]:
# Create the line plot. add_lineplot will return a list of length
# n_lines. In the case n_lines==1, then it will return a single value.
# These values are used to index each line on the line plot.
lines = animator.add_lineplot(
    n_lines=2, #add 2 lines to the plot
    color=['k','g'], #color then black and green
    tail=[33, -1], #give one a tail and the other infinite length
    line_style=['-','--'], #set the line styles to solid and dashed
    label=['Line 1','Line 2'], #name one Line 1 and the other Line 2
    y_lim=[0,1], #set the y limits of the plot to 0 to 1
    x_label='Index', #set the x axis label of the plot
    y_label='Value', #set the y axis label of the plot
    )
[4]:
# Create the bar chart. Similarly, add_barchart returns either a list of
# length n_bars or a single value.
bars = animator.add_barchart(
    n_bars=4, #add 4 bars to the chart
    color=['k', 'r', 'g', 'b'], #color each bar a unique color
    label=['Bar 1','Bar 2','Bar 3','Bar 4'], #name each bar
    x_lim=[-1.1, 1.1], #set the x limits of the plot to -1.1 to 1.1
    x_label='Number', #set the x axis label of the plot
    v_zero_line=True #add a thin vertical line at x=0
    )

Note that the animator GUI is not open even though we have initialized an instance of the Animator class and added plots to it. This is because the main GUI loop does not start until the start function is called. Once this function is called, it is up to the user to keep the GUI responsive. This is done by either calling the refresh function regularly, or call any of the functions that also refresh the animator. The functions that refresh the animator automatically are

  1. barchart_set_value

  2. lineplot_append_point

  3. lineplot_set_data

  4. reset

[5]:
# Start the animator. This will open the GUI and start the rendering thread
animator.start() # returns 0 on success
[5]:
0

Now we can start the animation loop. To update the line plot, we call the lineplot_append_point or lineplot_set_data functions. To denote which line on the plot we are updating the data of, we can use the return value of add_lineplot. A similary process is used for updating the bar chart with the barchart_set_value function.

[7]:
N = 200
for i in range(N):
    percent_done = i/(N-1)

    # Add a point at the current step index to both lines in the line plot
    animator.lineplot_append_point(lines[0], i, np.random.rand())
    animator.lineplot_append_point(lines[1], i, percent_done**2)

    # Set the values of each bar in the bar chart to something
    if i % 4 == 0: #Every fourth step update the bars
        animator.barchart_set_value(bars[0], percent_done**2)
        animator.barchart_set_value(bars[1], -percent_done**3)
        animator.barchart_set_value(bars[2], np.sin(np.pi*percent_done))
        animator.barchart_set_value(bars[3], np.random.rand()*2-1)
    time.sleep(0.05)

When done, terminate the animator to gracefully exit all children threads and close the GUI

[9]:
animator.terminate() # returns 0 on success
[9]:
0