2

from here:

Animation using matplotlib with subplots and ArtistAnimation

I got some hints on my question - however not enough. My problem:

I have two animations both somehow coupled and want to show each of them in a different subplot.

the first animation in the first subplot works fine - however the second (coupled to the first) animation in the second subplot just influences the first animation ...

So how do I decouple the subplots in a way that the second subplot doew NOT influence the first one:

here is the code of the example:

import math
from ClimateUtilities import *
import phys
import numpy as nm
import matplotlib.animation as animation
import matplotlib.pyplot as plt
from matplotlib import patches
#from matplotlib import animation


#------------Constants and file data------------
# 
printswitch = True
printswitch = False
printswitch2 = True
#printswitch2 = False

ECCabsoluteMax = 0.9
ECCmax = 0.067      # maximum value for this run - 
                    # should not be greater than
                    # ECCabsoluteMax
#ECCmax = 0.9       # maximum value for this run - should not be greater 
                    # than
                    # ECCabsoluteMax
if  ECCmax >= ECCabsoluteMax: 
    ECCmax = ECCabsoluteMax

ECCdelta = 0.001    # interval for graph

eccentricity = nm.arange(0., ECCmax, ECCdelta, dtype=float)
semimajorA = 1.0        # astronomical unit =~ 150.000.000 km mean        
                        # distance Sun Earth
totalRadN0 = 1370.      # radiation of Sun at TOA in Watt/m**2
albedoEarth = 0.3       # presently albedo of Earth, geographically 
                        # constant
T = 365.25              # duration of one orbit around central celestial 
                        # body in days
                        # here: duration of one orbit of Earth around Sun
R = 6378100.0           # radius of Earth in meters

TOIdim = ECCmax/ECCdelta
TOI = nm.arange(0., TOIdim, dtype=float ) 
                    # total insolation at location of Earth summed over 1 
                    # year
deltaT = 500        # ms interval of moving


# now define various "functions" like:

def computeTOI( ee, semimajorAxis, radiationAtStar, alpha  ):

    aa = semimajorAxis  # semimajor axis of orbital ellipse
    N0 = radiationAtStar# radiation of start at position of star (r = 0)
    resultTOI = 2.*nm.pi*T*R**2*N0*alpha/(aa**2*math.sqrt(1 - ee**2))
    return resultTOI

#
#####################################################################
#
print "start of ellipticity and absorbed insolation"
#
#
# Start of programme here
#
#####################################################################

# compute the various TOIs dependant on eccentricity "ecc"
#
ii = 0
for ecc in eccentricity:
    if printswitch:   print 'TOI = ', computeTOI( ecc, semimajorA,     
        totalRadN0, albedoEarth ), '\n'
    TOI[ii] = computeTOI( ecc, semimajorA, totalRadN0, 1. - albedoEarth 
                )/10.0**19
    ii = ii + 1

# TOI is an array consisting of TOIs depending on eccemtricity "ecc" 

x = eccentricity

if printswitch: print 'TOI = ', TOI
##########################################################################
# almost the whole screen is filled with this plot ... :)
##########################################################################

Main = plt.figure(figsize=(15.0,15.0))  
Main.subplots_adjust(top=0.95, left=0.09, right=0.95, hspace=0.20)

##########################################################################
axFigTOI = Main.add_subplot(211)     # first subplot

# Plot ... TOI over ECC: 

if ECCmax < 0.07: 
    plt.axis([0,0.07,8.9,9.0]) 

plt.title( 'Absorbed Irradiation and Orbital Eccentricity for Planet 
            Earth\n' )
plt.ylabel( 'Absorbed total \nsolar irradiation \n[Watt] *10**19' )
plt.xlabel( 'Eccentricity "e"' )

plt.plot( x, TOI, 'r-' )  # 'x' and 'TOI' are also center of "mini-
                          # ellipse"

# Now enter an ellipse here on Subplot 211 (first subplot) which slides 
# along curve:

xcenter, ycenter = x[1],TOI[1]      # center of ellipse to start with
width = 0.0025                      # width of small ellipse
height = 0.01                       # height of small ellipse

def init():                         # in order to initialize animation
    e1 = patches.Ellipse((xcenter, ycenter), width, height,\
        angle=0.0, linewidth=2, fill=False )

    axFigTOI.add_patch(e1)
    e1.set_visible( False )         # do not show (if True then ellipse 
                                    # stays here
    return [e1]

def animateEllipse(i):

    xcenter = x[i]
    ycenter = TOI[i]
    e1 = patches.Ellipse( ( xcenter, ycenter ), width, height,\
                     angle = 0.0, linewidth = 2, fill = True )
    if i == 1:
        e1.set_visible( True )

    axFigTOI.add_patch(e1)
    if printswitch: print 'i = ', i
    return [e1]

anim = animation.FuncAnimation( Main, 
                                animateEllipse, 
                                init_func=init, 
                                frames= int( TOIdim ), 
                                interval=deltaT,
                                blit=True )

#########################################################################
# the second subplot in the first figure for size of ellipse depending on 
# ECC
#########################################################################

# we still have a problem to get the "patch" (Ellipse) into the 2nd 
# subplot ...


axFigEllipse = Main.add_subplot(212)

plt.title( 'Shape of an Ellipse due to eccentricity' )
plt.ylabel( 'Height of Ellipse' )
plt.xlabel( 'Constant Semi-major Axis' )
"""
# 
# create an ellipse with following parameters - to be changed later for 
# curve
#   values
#


xcenter2 = x[40]
ycenter2 = TOI[40]      # center of ellipse 2 to start with
width2 = 0.0125
height2 = 0.0115

ell2 = patches.Ellipse( ( xcenter2, ycenter2 ), width2, height2,\
      angle=0.0, linewidth=2, fill=False )

ell2.set_visible(True)
axFigEllipse.add_patch(ell2)

#"""
"""

def init212():                         # in order to initialize animation
    ell2 = patches.Ellipse((xcenter2, ycenter2), width2, height2,\
        angle=0.0, linewidth=2, fill=False )

    axFigEllipse.add_patch(ell2)
    ell2.set_visible( False )         # do not show (if True then ellipse 
                                      # stays here
    return [ell2]

def animateEllipse(jj):

    #xcenter2 = xcenter2 + jj/10**4
    #ycenter2 = ycenter2 + jj/10**4
    ell2 = patches.Ellipse((xcenter2, ycenter2), width2, height2,\
         angle=0.0, linewidth=2, fill=True, zorder=2)
    if jj == 1:
        ell2.set_visible(True)

    axFigEllipse.add_patch(ell2)

    return [ell2]



anim = animation.FuncAnimation( Main, animateEllipse, 
                               init_func=init212, 
                               frames=360, 
                               interval=20,
                               blit=True )


#anim = animation.FuncAnimation(figEllipse, animateEllipse,     
        init_func=init_Ellipse, interval=1, blit=True)
#"""
plt.show()

Now when I remove the """ then there is only the red line visible ... no activity ...

Community
  • 1
  • 1
kampmannpeine
  • 129
  • 1
  • 2
  • 11

1 Answers1

1

In your code, you essentially redefine animateEllipse later. You should only call a single animate in a script which should update both ellipses (return both handles from the objects). For resizing you can just use the existing ellipse handle but moving appears to need a new ellipse to be added. I couldn;t get your code working but as a minimal example a function to update all subplots (these can each be in their own functions).

import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse
from matplotlib import animation
import numpy as np

fig = plt.figure()
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)
e1 = Ellipse(xy=(0.5, 0.5), width=0.5, height=0.5, angle=0)
e2 = Ellipse(xy=(0.5, 0.5), width=0.5, height=0.5, angle=0)
ax1.add_patch(e1)
ax2.add_patch(e2)

def init():
    e1.set_visible(False)
    e2.set_visible(False)
    return e1,e2

def animateEllipse211(i):
    e1 = Ellipse(xy=(0.5+0.2*np.sin(i/200.), 0.5+0.2*np.sin(i/200.)), width=0.5, height=0.5, angle=0)
    ax1.add_patch(e1)
    if i==0:
        e1.set_visible(True)

    return e1

def animateEllipse212(i):
    if i==0:
        e2.set_visible(True)
    e2.width  = 0.5*np.sin(i/200.)
    e2.height = 0.5*np.sin(i/200.)

    return e2

def animate(i):

    e1 = animateEllipse211(i)
    e2 = animateEllipse212(i)

    return e1,e2

anim = animation.FuncAnimation(fig, animate, init_func=init, interval=1, blit=True)
plt.show()

UPDATE: I'm not sure why this strange init problem occurs but think it has been noted on a few other posts (this and this) to be due to using blit=True. The animation on matplotlib is a little rough around the edges and certainly isn't very intuitive. Worse than that, the choice of backend (i.e. what plots the actual data) makes a difference to the way it works. Personally I normally run a loop, use interactive mode and save figures if I need a video.

import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse
import numpy as np

#Setup figure, add subplots and ellipses
fig = plt.figure()
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)
e1 = Ellipse(xy=(0.5, 0.5), width=0.5, height=0.5, angle=0)
e2 = Ellipse(xy=(0.5, 0.5), width=0.5, height=0.5, angle=0)
ax1.add_patch(e1)
ax2.add_patch(e2)

#Plot Red line
ax1.plot(np.linspace(.3,.7,100),np.linspace(.3,.7,100),'r-')

#Turn on interactive plot
plt.ion()
plt.show()

#Define a loop and update various
for i in range(0, 10000, 10):

    print(i)
    #Update ellipse 1
    e1.remove()
    e1 = Ellipse(xy=(0.5+0.2*np.sin(i/200.), 
                     0.5+0.2*np.sin(i/200.)), 
                 width=0.5, height=0.5, angle=0)
    ax1.add_patch(e1)

    #Update ellipse 2
    e2.width  = 0.5*np.sin(i/200.)
    e2.height = 0.5*np.sin(i/200.)

    plt.draw()
    plt.pause(0.0001)
Community
  • 1
  • 1
Ed Smith
  • 12,716
  • 2
  • 43
  • 55
  • thank you, dear Ed, prima vista it is similar to what I wanted ... I just copied it and put it into Enthought Cannopy and your version just runs - gorgeously Jörg btw: I am looking for a model which simulates greenhouse effect in Python - I found something at University of Colorado - but that is in Java ... however it is almost that what I need ... (only not in Python plus matplotlib :) ) – – kampmannpeine Nov 13 '15 at 14:50
  • Ed, I still have problems - and while those, I discovered that your routine "init()" will be called **two times**. I noticed that while inputting a **print** into init(). Do you know why? – kampmannpeine Nov 15 '15 at 20:22
  • and my init() will be called three times - any explanation? – kampmannpeine Nov 15 '15 at 20:24
  • Hi @kampmannpeine I'm not sure why it's called twice but think it's a result of using blitting (see http://stackoverflow.com/questions/22178086/funcanimations-init-function-getting-called-2-times-instead-of-once). Maybe setting `blit=False` will help (although the code may run a bit slower). The need to `set_visible(False)` in the `init` seems like a hack, I got it from http://stackoverflow.com/questions/21439489/matplotlib-animation-first-frame-remains-in-canvas-when-using-blit which again appears to be a problem with blit. – Ed Smith Nov 15 '15 at 23:01