python animation

Concerning processing components: filters, file load/save, visualizations, communication ...
Post Reply
Posts: 27
Joined: Wed Nov 14, 2018 6:10 pm

python animation

Post by RadhaKumari »

I have a gauge animation indicating the power received. Ideally it should work in real-time. But, after initializing it and expecting it to change value as the program proceeds , I am stuck because the stimulations won't start until I close the figure(which was supposed to change values as the program proceeds). The code is below. I do not know how to get it to work in real-time. Also, is it possible for the figure to appear as part of the visualization window and not separately, it will be so convenient if this is possible for matplotlib figures to be embedded into the main visualization window:

Code: Select all

import os, sys
import matplotlib
from matplotlib import cm
from matplotlib import pyplot as plt
import numpy as np
import matplotlib.animation as animation
import time

from matplotlib.patches import Circle, Wedge, Rectangle
# let's define a new box class that inherits from OVBox
class MyOVBox(OVBox):
    def __init__(self):
        # we add a new member to save the signal header information we will receive
        self.signalHeader = None
        #stores the current chunk at anytime    
    def initialize(self):
    def animate(self,i):
        if (self.chunk[0]<=0.33*self.threshold):
        elif (self.chunk[0]<=0.66*self.threshold) and (self.chunk[0]>0.33*self.threshold):

        elif (self.chunk[0]<=self.threshold) and (self.chunk[0]>0.66*self.threshold):
        elif (self.chunk[0]<=1.33*self.threshold) and (self.chunk[0]>self.threshold):

        elif (self.chunk[0]<1.66*self.threshold) and (self.chunk[0]>1.33*self.threshold):

        else :
    def degree_range(self,n): 
        start = np.linspace(0,180,n+1, endpoint=True)[0:-1]
        end = np.linspace(0,180,n+1, endpoint=True)[1::]
        mid_points = start + ((end-start)/2.)
        return np.c_[start, end], mid_points

    def rot_text(self,ang): 
        rotation = np.degrees(np.radians(ang) * np.pi / np.pi - np.radians(90))
        return rotation

    def gauge(self,arrow=1): 
        #fixed stuff
        title='ERD level'
        N = len(labels)
        #if colors is a string, we assume it's a matplotlib colormap and we discretize in N discrete colors 
        if isinstance(colors, str):
           cmap = cm.get_cmap(colors, N)
           cmap = cmap(np.arange(N))
           colors = cmap[::-1,:].tolist()
        if isinstance(colors, list): 
            if len(colors) == N:
                colors = colors[::-1]
                raise Exception("\n\nnumber of colors {} not equal to number of categories{}\n".format(len(colors), N))

        ang_range, mid_points = self.degree_range(N)
        labels = labels[::-1]
        patches =[]
        for ang, c in zip(ang_range, colors): 
            # sectors
            patches.append(Wedge((0.,0.), .4, *ang, facecolor='w', lw=2))
            # arcs
            patches.append(Wedge((0.,0.), .4, *ang, width=0.10, facecolor=c, lw=2, alpha=0.5))
            [ for p in patches]

        #set the labels (e.g. 'LOW','MEDIUM',...)
        for mid, lab in zip(mid_points, labels): 
   * np.cos(np.radians(mid)), 0.35 * np.sin(np.radians(mid)), lab, horizontalalignment='center', verticalalignment='center', fontsize=14,             fontweight='bold', rotation = self.rot_text(mid))

        #set the bottom banner and the title
        r = Rectangle((-0.4,-0.1),0.8,0.1, facecolor='w', lw=2), -0.05, title, horizontalalignment='center',verticalalignment='center', fontsize=22, fontweight='bold')
        pos = mid_points[abs(arrow - N)], 0, 0.225 * np.cos(np.radians(pos)), 0.225 * np.sin(np.radians(pos)),width=0.04, head_width=0.09, head_length=0.1, fc='k', ec='k'), 0), radius=0.02, facecolor='k')), 0), radius=0.01, facecolor='w', zorder=11))
        #removes frame and ticks, and makes axis equal and tight[])[])'equal')
    def process(self):

        for chunkIndex in range( len(self.input[0]) ):
            if(type(self.input[0][chunkIndex]) == OVSignalBuffer):
            elif(type(self.input[0][chunkIndex]) == OVSignalEnd):
                print 'Signal end reached'


# Finally, we notify openvibe that the box instance 'box' is now an instance of MyOVBox.
# Don't forget that step !!
box = MyOVBox()

Posts: 144
Joined: Wed Oct 31, 2018 9:14 am

Re: python animation

Post by Thibaut »

I have never used matplotlib for animation, but I think it must be pretty heavy as a system. Personally, I made a system as you describe it, but I coded it in C ++ in a specific box. For visualizations, I think you should turn to the visualization box implementation. Or use existing ones by passing them the information calculated by your script.
If you go about the implementation of a box, the Graz visualization displays a blue bar that evolves according to the data received (input amplitude) it could help you.

Post Reply