Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Matplotlib-动画:如何在两个子地块内制作两个耦合动画?_Python_Animation_Matplotlib_Plot_Subplot - Fatal编程技术网

Python Matplotlib-动画:如何在两个子地块内制作两个耦合动画?

Python Matplotlib-动画:如何在两个子地块内制作两个耦合动画?,python,animation,matplotlib,plot,subplot,Python,Animation,Matplotlib,Plot,Subplot,从这里开始: 关于我的问题,我得到了一些提示——但还不够。我的问题是: 我有两个动画,它们以某种方式耦合在一起,并希望在不同的子地块中显示它们 第一个子地块中的第一个动画工作正常-但是第二个子地块中的第二个(耦合到第一个)动画只影响第一个动画 那么,如何以第二个子地块不影响第一个子地块的方式解耦子地块: 下面是示例的代码: import math from ClimateUtilities import * import phys import numpy as nm import matpl

从这里开始:

关于我的问题,我得到了一些提示——但还不够。我的问题是:

我有两个动画,它们以某种方式耦合在一起,并希望在不同的子地块中显示它们

第一个子地块中的第一个动画工作正常-但是第二个子地块中的第二个(耦合到第一个)动画只影响第一个动画

那么,如何以第二个子地块不影响第一个子地块的方式解耦子地块:

下面是示例的代码:

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()
导入数学
从气候设备进口*
进口物理
将numpy导入为nm
将matplotlib.animation导入为动画
将matplotlib.pyplot作为plt导入
从matplotlib导入修补程序
#从matplotlib导入动画
#------------常量和文件数据------------
# 
printswitch=True
printswitch=False
printswitch2=True
#printswitch2=False
ECC绝对最大值=0.9
ECCmax=0.067#此运行的最大值-
#不应大于
#绝对最大值
#ECCmax=0.9#此运行的最大值-不应大于
#
#绝对最大值
如果ECCmax>=ecccabsolutemax:
ECCmax=ECCmax
ECCdelta=图的0.001#间隔
偏心率=nm.arange(0,ECCmax,ECCdelta,dtype=float)
semimajorA=1.0#天文单位=~150.000.000 km平均值
#日地距离
总RADN0=1370.#TOA处的太阳辐射,单位为瓦特/米**2
albedoEarth=0.3#目前地球的反照率,地理位置
#不变的
T=365.25#围绕中心天体的一个轨道的持续时间
#几天内的尸体
#这里是地球绕太阳一周的持续时间
R=6378100.0#地球半径(米)
TOIdim=ECCmax/ECCdelta
TOI=nm.arange(0,TOIdim,dtype=float)
#地球位置的总日照总和超过1
#年
deltaT=500#ms移动间隔
#现在定义各种“功能”,如:
def computeTOI(ee、半主流、辐射恒星、阿尔法):
aa=半长轴#轨道椭圆的半长轴
N0=辐射恒星#恒星起始位置的辐射(r=0)
结果i=2.*nm.pi*T*R**2*N0*alpha/(aa**2*math.sqrt(1-ee**2))
返回结果
#
#####################################################################
#
打印“椭圆度和吸收日照开始”
#
#
#节目在这里开始
#
#####################################################################
#根据偏心率“ecc”计算各种TOI
#
ii=0
对于偏心率下的ecc:
如果printswitch:print'TOI=',computeTOI(ecc,semimajorA,
totalRadN0,albedoEarth),“\n”
TOI[ii]=计算机(ecc,半马略岛,总辐射0,1.-阿尔贝多阿尔斯)
)/10.0**19
ii=ii+1
#TOI是由TOI组成的数组,取决于ecc属性“ecc”
x=偏心率
如果printswitch:print'TOI=',TOI
##########################################################################
#几乎整个屏幕都充满了这样的情节……:)
##########################################################################
Main=plt.图(figsize=(15.0,15.0))
主。子批次调整(顶部=0.95,左侧=0.09,右侧=0.95,hspace=0.20)
##########################################################################
axFigTOI=Main.add_子批次(211)#第一个子批次
#情节。。。基于ECC的TOI:
如果ECCmax<0.07:
plt.轴([0,0.07,8.9,9.0])
plt.标题(“行星的吸收辐射和轨道偏心率
地球\n')
plt.ylabel('吸收总辐射量\n[瓦特]*10**19')
plt.xlabel('偏心率“e')
plt.plot(x,TOI,'r-')#'x'和'TOI'也是“mini”的中心-
#椭圆“
#现在在滑动的子地块211(第一个子地块)上输入一个椭圆
#沿曲线:
xcenter,ycenter=x[1],TOI[1]#开始椭圆的中心
宽度=0.0025#小椭圆的宽度
高度=0.01#小椭圆的高度
def init():#以初始化动画
e1=面片。椭圆((X中心,Y中心),宽度,高度\
角度=0.0,线宽=2,填充=假)
axFigTOI.添加补丁(e1)
e1.设置_可见(假)#不显示(如果为真,则为椭圆
#留在这里
返回[e1]
def animateEllipse(i):
xcenter=x[i]
ycenter=TOI[i]
e1=面片。椭圆((X中心,Y中心),宽度,高度\
角度=0.0,线宽=2,填充=真)
如果i==1:
e1.设置_可见(真)
axFigTOI.添加补丁(e1)
如果printswitch:print'i=',i
返回[e1]
anim=animation.FuncAnimation(主,
动画片,
init_func=init,
帧=int(TOIdim),
间隔=延迟,
blit=真)
#########################################################################
#第一个图形中的第二个子图,用于显示椭圆的大小,具体取决于
#ECC
#########################################################################
#我们仍然有一个问题,让“补丁”(椭圆)进入第二
#子地块。。。
axFigEllipse=Main.add_子批次(212)
plt.title(‘偏心引起的椭圆形状’)
plt.ylabel(‘椭圆高度’)
plt.xlabel(‘恒定半长轴’)
"""
# 
#使用以下参数创建一个椭圆-稍后更改
#曲线
#价值观
#
xcenter2=x[40]
ycenter2=TOI[40]#椭圆2的中心以
宽度2=0.0125
高度2=0.0115
ell2=面片。椭圆((X中心2,Y中心2),宽度2,高度2\
角度=0.0,线宽=2,
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()
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)