1

I am a somehow new Python user, and completely new related to matplotlib environment. In the following code, I have managed to create an animation, in which I read some csv files, and I plot animated results either by barplots ot time domain ones. The code is following:

import csv
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
from matplotlib.animation import FuncAnimation, writers, ImageMagickFileWriter
'''



fig, axs = plt.subplots(2,2) #subplots is a module that returns 1 figure with a list of two axes (due to the two in the arguments) 


my_data =np.genfromtxt('1b. freq (time domain).csv', delimiter=',',skip_header=1)
my_data_bar =np.genfromtxt('1a.powers_loads_generators (bar_plots).csv', delimiter=',',skip_header=1)

my_data_2=np.genfromtxt('2b.angles (time domain).csv', delimiter=',',skip_header=1)
my_data_bar_2=np.genfromtxt('2a.torques (bar_plots).csv', delimiter=',',skip_header=1)



axs[0][1].set_ylim(49.5, 50.5)
axs[0][1].set_xlim(0, 10)

axs[0][1].set_xlabel('Time (Seconds)')
axs[0][1].set_ylabel('Frequency (Hz)')

frequency_lines = axs[0][1].plot([],[]) # tuple of lines. 

######################################################################################################################################################################################################################    


axs[0][0].set_xlim(0, 400) 


testNames=('G1', 'G2', 'WG1', 'WG3','TOTG','LDA','LDB','LDC','TOTLD')


axs[0][0].invert_yaxis()  # labels read top-to-bottom
axs[0][0].set_xlabel('Active Power (MW)')


bar_containers= axs[0][0].barh(range(9),my_data_bar[0,1:], align='center', height=0.5, tick_label=testNames) 
bar_containers[4].set_color('r')
bar_containers[8].set_color('r')       


######################################################################################################################################################################################################################   

axs[1][1].set_ylim(0, 100)
axs[1][1].set_xlim(0, 10)

axs[1][1].set_xlabel('Time (Seconds)')
axs[1][1].set_ylabel('Rotor Angles Difference (rads)')

angle_lines = axs[1][1].plot([],[]) 

######################################################################################################################################################################################################################    


axs[1][0].set_xlim(0,1) 
testNames=('Tmech1', 'Tel1', 'Tmech2', 'Tel2')


axs[1][0].invert_yaxis()  
axs[1][0].set_xlabel('Torques (pu)')





bar_containers2= axs[1][0].barh(range(4),my_data_bar_2[0,1:], align='edge', height=0.5, tick_label=testNames)  #arguments list of position and height  and returns a container   



######################################################################################################################################################################################################################    


totFrames=len(my_data)





def update(index):#receive a variable to understand which frame  we take

        print ("Update",index)
        frequency_lines[0].set_data(my_data[:index+1, :].T) 
        angle_lines[0].set_data(my_data_2[:index+1,0],my_data_2[:index+1,4])    


        for i, b in enumerate(bar_containers):
            b.set_width(my_data_bar[index,i+1])

        for i, b in enumerate(bar_containers2):
            b.set_width(my_data_bar_2[index,i+1])   


ani = FuncAnimation(fig, update, frames = range(totFrames), interval=1) #how many ms are between the frames
ani.save('visualization.gif', writer='imagemagick')

My questions are: 1) How I could add some delays between frames? For instance I would like the interval variable in the FuncAnimation command to be a variable. For Frames 0-100 the time delay, I would like to be 1, for Frames 100-200, I would like the delay between the frames to be 600 (so as to focus into these time zone), and from frame number 200 - totFrames the delay between frames to be 1 again.

2) When I save the .ani, I end up with a .gif file which the legends and labels crowding each other. However, if I maximize the animation in full screen mode from Python then the subplots in Python are looking fine. How I could save the animation file,in full screen, so as the .gif file to have subplots that do not intesect each other?

Thank you in advance

merv
  • 67,214
  • 13
  • 180
  • 245
StF
  • 11
  • 1
  • the Python animation when full screen is used can be seen here: https://i.stack.imgur.com/gx7Ps.png the squeezed stored .gif file can be seen here: https://i.stack.imgur.com/zbrBu.png – StF Sep 22 '19 at 13:40
  • 2) You need to change the figure size as in [this question](https://stackoverflow.com/questions/332289/how-do-you-change-the-size-of-figures-drawn-with-matplotlib); 1) There is no dedicated API for changing the interval in the fly, but you can still manipulate the underlying event source as shown in [this question](https://stackoverflow.com/questions/41882736/can-i-change-the-interval-of-a-previously-created-matplotlib-funcanimation) – ImportanceOfBeingErnest Sep 22 '19 at 13:49

0 Answers0