【问题标题】:plotting animation of different dune lee angles绘制不同沙丘背风角的动画
【发布时间】:2021-02-17 00:37:22
【问题描述】:

我正在写关于沙丘上水流模式的硕士论文。我正在研究具有不同沙丘背风角的沙丘对流动模式的影响。我将测量沙丘上方的速度剖面。沙丘的形状包括一个固定的沙丘stoss斜坡(缓和的向上斜坡),直到10厘米的固定高度的沙丘顶部,然后在9厘米的固定高度朝向沙丘边缘的一个固定的缓坡向下斜坡,然后是一个更陡峭的斜坡,朝向沙丘的不同背风坡。沙丘被固定到临界点,因此下风坡的角度从 15 到 30 度不等,下风坡在沙丘处结束,高度为 0 厘米,但水平距离因此而变化。 我想制作具有不同沙丘背风角的不同沙丘的动画,我已经从所有沙丘中制作了子图subplots dunes 但我正在努力将这些子图转换为动画。我有。尝试了 matplotlib 中的动画工具(FuncAnimation),但惨遭失败。我的目标是展示每个沙丘 2 秒并添加一个图例。

获取子图的代码是:

import matplotlib.pyplot as plt
from math import *
import numpy as np
from matplotlib import pyplot as plt

fig, axs = plt.subplots(8,2, sharex= 'col', sharey ='row', gridspec_kw={'hspace': 1.05, 'wspace': 0.1})
(ax1, ax2), (ax3, ax4), (ax5, ax6) , (ax7, ax8), (ax9, ax10), (ax11, ax12), (ax13, ax14) , (ax15, ax16) = axs
fig.suptitle('measurements velocity profiles above dunes')
ax1.set_title('dune lee: 15 degrees')
ax2.set_title('dune lee: 16 degrees')
ax3.set_title('dune lee: 17 degrees')
ax4.set_title('dune lee: 18 degrees')
ax5.set_title('dune lee: 19 degrees')
ax6.set_title('dune lee: 20 degrees')
ax7.set_title('dune lee: 21 degrees')
ax8.set_title('dune lee: 22 degrees')
ax9.set_title('dune lee: 23 degrees')
ax10.set_title('dune lee: 24 degrees')
ax11.set_title('dune lee: 25 degrees')
ax12.set_title('dune lee: 26 degrees')
ax13.set_title('dune lee: 27 degrees')
ax14.set_title('dune lee: 28 degrees')
ax15.set_title('dune lee: 29 degrees')
ax16.set_title('dune lee: 30 degrees')

duneheight = 10 # fixed dunetop
dunebrinkpoint = float(0.9*duneheight) #fixed dunebrinkpoint
brinkpointgradient = float(tan(15*pi/180)) #varying lee slope, now 15 degrees 
crestgradient = float(tan(15*pi/180))/4 #fixed gentle upward stoss side lope
stossgradient = float(tan(15*pi/180))/4 #fixed gentle downward slope from dune top towards brinkpoint

ax1.plot([0, duneheight/stossgradient, duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient,duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient+dunebrinkpoint/brinkpointgradient,
         duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient+dunebrinkpoint/brinkpointgradient + duneheight/stossgradient], [0, duneheight, dunebrinkpoint, 0, duneheight], 'r-',
         )
        
brinkpointgradient = float(tan(16*pi/180)) #varying lee slope, now 16 degrees

ax2.plot([0, duneheight/stossgradient, duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient,duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient+dunebrinkpoint/brinkpointgradient,
         duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient+dunebrinkpoint/brinkpointgradient + duneheight/stossgradient], [0, duneheight, dunebrinkpoint, 0, duneheight], 'y-',
         )

brinkpointgradient = float(tan(17*pi/180)) #varying lee slope, now 17 degrees

ax3.plot([0, duneheight/stossgradient, duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient,duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient+dunebrinkpoint/brinkpointgradient,
         duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient+dunebrinkpoint/brinkpointgradient + duneheight/stossgradient], [0, duneheight, dunebrinkpoint, 0, duneheight], 'g-',
         )

brinkpointgradient = float(tan(18*pi/180)) #varying lee slope, now 18 degrees

ax4.plot([0, duneheight/stossgradient, duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient,duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient+dunebrinkpoint/brinkpointgradient,
         duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient+dunebrinkpoint/brinkpointgradient + duneheight/stossgradient], [0, duneheight, dunebrinkpoint, 0, duneheight], 'tab:brown',
        )


brinkpointgradient = float(tan(19*pi/180)) #varying lee slope, now 19 degrees

ax5.plot([0, duneheight/stossgradient, duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient,duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient+dunebrinkpoint/brinkpointgradient,
         duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient+dunebrinkpoint/brinkpointgradient + duneheight/stossgradient], [0, duneheight, dunebrinkpoint, 0, duneheight], 'tab:orange',
        )

brinkpointgradient = float(tan(20*pi/180)) #varying lee slope, now 20 degrees

ax6.plot([0, duneheight/stossgradient, duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient,duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient+dunebrinkpoint/brinkpointgradient,
         duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient+dunebrinkpoint/brinkpointgradient + duneheight/stossgradient], [0, duneheight, dunebrinkpoint, 0, duneheight], 'b-',
        )

brinkpointgradient = float(tan(21*pi/180)) #varying lee slope, now 21 degrees

ax7.plot([0, duneheight/stossgradient, duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient,duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient+dunebrinkpoint/brinkpointgradient,
         duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient+dunebrinkpoint/brinkpointgradient + duneheight/stossgradient], [0, duneheight, dunebrinkpoint, 0, duneheight], 'tab:purple',
        )

brinkpointgradient = float(tan(22*pi/180)) #varying lee slope, now 22 degrees

ax8.plot([0, duneheight/stossgradient, duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient,duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient+dunebrinkpoint/brinkpointgradient,
         duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient+dunebrinkpoint/brinkpointgradient + duneheight/stossgradient], [0, duneheight, dunebrinkpoint, 0, duneheight], 'tab:grey',
        )


brinkpointgradient = float(tan(23*pi/180)) #varying lee slope, now 23 degrees

ax9.plot([0, duneheight/stossgradient, duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient,duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient+dunebrinkpoint/brinkpointgradient,
         duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient+dunebrinkpoint/brinkpointgradient + duneheight/stossgradient], [0, duneheight, dunebrinkpoint, 0, duneheight], 'r-',
        )
        
brinkpointgradient = float(tan(24*pi/180)) #varying lee slope, now 24 degrees

ax10.plot([0, duneheight/stossgradient, duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient,duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient+dunebrinkpoint/brinkpointgradient,
         duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient+dunebrinkpoint/brinkpointgradient + duneheight/stossgradient], [0, duneheight, dunebrinkpoint, 0, duneheight], 'y-',
        )

brinkpointgradient = float(tan(25*pi/180)) #varying lee slope, now 25 degrees

ax11.plot([0, duneheight/stossgradient, duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient,duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient+dunebrinkpoint/brinkpointgradient,
         duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient+dunebrinkpoint/brinkpointgradient + duneheight/stossgradient], [0, duneheight, dunebrinkpoint, 0, duneheight], 'g-',
        )

brinkpointgradient = float(tan(26*pi/180)) #varying lee slope, 26 degrees

ax12.plot([0, duneheight/stossgradient, duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient,duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient+dunebrinkpoint/brinkpointgradient,
         duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient+dunebrinkpoint/brinkpointgradient + duneheight/stossgradient], [0, duneheight, dunebrinkpoint, 0, duneheight], 'tab:brown',
        )

brinkpointgradient = float(tan(27*pi/180)) #varying lee slope, 27 degrees

ax13.plot([0, duneheight/stossgradient, duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient,duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient+dunebrinkpoint/brinkpointgradient,
         duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient+dunebrinkpoint/brinkpointgradient + duneheight/stossgradient], [0, duneheight, dunebrinkpoint, 0, duneheight], 'tab:orange',
        )

brinkpointgradient = float(tan(28*pi/180)) #varying lee slope, 28 degrees

ax14.plot([0, duneheight/stossgradient, duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient,duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient+dunebrinkpoint/brinkpointgradient,
         duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient+dunebrinkpoint/brinkpointgradient + duneheight/stossgradient], [0, duneheight, dunebrinkpoint, 0, duneheight], 'b-',
        )

brinkpointgradient = float(tan(29*pi/180)) #varying lee slope, 29 degrees

ax15.plot([0, duneheight/stossgradient, duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient,duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient+dunebrinkpoint/brinkpointgradient,
         duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient+dunebrinkpoint/brinkpointgradient + duneheight/stossgradient], [0, duneheight, dunebrinkpoint, 0, duneheight], 'tab:purple',
        )

brinkpointgradient = float(tan(30*pi/180)) #varying lee slope, 30 degrees

ax16.plot([0, duneheight/stossgradient, duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient,duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient+dunebrinkpoint/brinkpointgradient,
         duneheight/stossgradient+(duneheight-dunebrinkpoint)/crestgradient+dunebrinkpoint/brinkpointgradient + duneheight/stossgradient], [0, duneheight, dunebrinkpoint, 0, duneheight], 'tab:grey',
        )
plt.show()

【问题讨论】:

  • 这里是泥沙运输工程师 -- 你实际上是想制作什么动画?
  • 你是想把subplot一个一个动画化还是一起animate 好吗?
  • 我要一个一个动画,时间步长为2秒,@Jay Patel 谢谢你的回复!
  • @Paul H:我想以 2 秒的时间步长为子图中表示的各个沙丘设置动画,当下一个沙丘出现时,前一个沙丘应该消失。最重要的是,感谢您的回复!

标签: python matplotlib plot jquery-animate


【解决方案1】:

好的,首先,我将为您提供一些有关原始代码的提示。这是我在写关于沉积物输送的硕士论文时希望得到的信息。

基本上:不要用代码重复自己

如果您发现自己在复制/粘贴内容,则需要编写循环或函数,可能两者兼而有之。 在我们的例子中,您的原始代码可以短至:

import matplotlib.pyplot as plt
from math import *
import numpy as np
from matplotlib import pyplot as plt

fig, axs = plt.subplots(8, 2, sharex='col', sharey='row',
                        figsize=(10, 10),
                        gridspec_kw={'hspace': 1.05, 'wspace': 0.1})
fig.suptitle('measurements velocity profiles above dunes')

duneheight = 10 # fixed dunetop
dunebrinkpoint = float(0.9 * duneheight) 
crestgradient = float(tan(15 * pi / 180)) / 4 
stossgradient = float(tan(15 * pi / 180)) / 4 

dunelees = np.arange(15, 31)
for ax, dlee in zip(axs.flat, dunelees):
    ax.set_title(f"dune lee: {dlee} degrees")
    brinkpointgradient = float(tan(dlee*pi/180)) #varying lee slope, now 15 degrees

    x1 = duneheight / stossgradient
    x2 = x1 + (duneheight - dunebrinkpoint) / crestgradient
    x3 = x2 + dunebrinkpoint / brinkpointgradient
    x4 = x3 + x1 
    x = [0, x1, x2, x3, x4]
    y = [0, duneheight, dunebrinkpoint, 0, duneheight]
    ax.plot(x, y, linestyle='-')

其次,我注意到您在 Crest 和 Stoss 渐变的计算中硬编码了 15。我认为这是对的,但请仔细检查这些不应该随背风角而变化。

现在是动画。我通过直接从 matplotlib 的文档中直接复制/粘贴来构建下面的代码。当然,您不能通过复制和粘贴来撰写硕士论文,但您需要能够阅读您正在使用的工具的文档。所以我建议现在就养成一些好习惯。无论您是继续攻读博士学位还是成为一名执业工程师,它都会为您服务。

文档中的示例:https://matplotlib.org/stable/gallery/animation/animate_decay.html#sphx-glr-gallery-animation-animate-decay-py

from math import tan, pi

import numpy
from matplotlib import pyplot, animation

fig, ax = pyplot.subplots(figsize=(8, 3))
fig.suptitle('measurements velocity profiles above dunes')
dune, = ax.plot([], [], lw=2, color='k')
ax.set_xlim(0, 400)
ax.set_ylim(0, 15)
xdata, ydata = [], []


def init():
    del xdata[:]
    del ydata[:]
    dune.set_data(xdata, ydata)
    return dune,

def dune_xs():
    duneheight = 10 
    dunebrinkpoint = float(0.9 * duneheight)
    crestgradient = float(tan( 15 * pi / 180)) / 4
    stossgradient = float(tan(15 * pi / 180)) / 4 

    x1 = duneheight / stossgradient
    x2 = x1 + (duneheight - dunebrinkpoint) / crestgradient
    for lee in numpy.arange(15, 31):
        brinkpointgradient = float(tan(lee* pi / 180))
        x3 = x2 + dunebrinkpoint / brinkpointgradient
        x4 = x3 + x1
        yield [0, x1, x2, x3, x4], [0, duneheight, dunebrinkpoint, 0, duneheight], lee
        
def run(data):
    # update the data
    x, y, lee = data
    dune.set_data(x, y)
    ax.set_title(f"Lee angle: {lee} degrees")
    return dune,

ani = animation.FuncAnimation(fig, run, dune_xs, interval=120, init_func=init)
fig.tight_layout()
pyplot.show()
ani.save('dunes.gif', writer='pillow', fps=30)

【讨论】:

  • 感谢您的回复!沙丘坡度和波峰坡度确实不应该变化!我将尝试从您的示例中更改一些代码以获得最佳条件,但我非常感谢,这对我有很大帮助!我是 python 的相对新手,在 stackoverflow 上是全新的,这种支持对我有很大帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-23
  • 1970-01-01
  • 1970-01-01
  • 2017-06-15
  • 1970-01-01
  • 2012-01-01
相关资源
最近更新 更多