如何绘制动画图?

时间:2018-11-06 18:46:40

标签: python animation matplotlib plot graph

enter image description here

enter image description here

上面的图像是在给定载荷,载荷的长度和位置的作用下,梁的剪切力和弯矩图。

帮助我随着负载位置的变化绘制运动图

看来我可以使用“ matplot.animation” ,但是编码是如此困难。

我尝试了一些例子,但是失败了。

我使用了python 3.7

from numpy import *
import matplotlib.pyplot as plt
from matplotlib import animation

P=100    # = load
L=10     # = Length of Beam
LS=8     # = posiotion of load<- **I want to change the value from 0 to 10 by 0.01.**

R1 = P * (L - LS) / (L)
R2 = P * LS / (L)

SF1 = R1
SF2 = R2

# 보
x_beam = arange(0, L, .01)
p_beam = x_beam * 0

# 좌측 절점에서 하중점까지
x1 = arange(0, LS, .01)
p1 = (x1 * 0 + R1)

# 하중점에서의 전단력 변화★
x2 = arange(R1, -R2, -.01)
p2 = x2 * 0 + LS

# 하중점에서 우측 절점까지
x3 = arange(LS, L, .01)
p3 = -(x3 * 0 + R2)

# 모멘트
bend1 = R1 * x1
bend2 = R1 * x3 - P * (x3 - LS)

#################################    ### 그래프
plt.figure(figsize=(10, 8))  # 그래프 사이즈
plt.subplots_adjust(hspace=0.5)  # left,right,bottom,top,wspace,hspace


### Shear Force Diagram
plt.subplot(2, 1, 1)
# 보
plt.plot(x_beam, p_beam, '4b')

# 하중점 제외한 부분에서의 전단력 변화
plt.plot(x1, p1, 'r', x3, p3, 'r')  # 가로, 세로 순서

# 하중점에서의 전단력 변화
plt.plot(p2, x2, 'r')

plt.xlim(0, L)
plt.ylim(-P - 10, P + 10)
plt.xlabel('Length')
plt.ylabel('Shear Force')
plt.title('Shear Force Diagram')


### Bending Moment Diagram
plt.subplot(2, 1, 2)
plt.xlim(0, L)
plt.plot(x_beam, p_beam, '4b')
plt.plot(x1, bend1, 'r', x3, bend2, 'r')
plt.xlabel('Length')
plt.ylabel('Moment')
plt.title('Bending Moment Diagram')
plt.show()

1 个答案:

答案 0 :(得分:0)

您可以检查我的悬臂梁动画编码:

在Python 3.7中具有matplotlib.animation的悬臂梁动画:

"""
Cantilever beam bending 
Analytical solution animation.
Assumptions:
 - small deformations, elastic bending
 - no force vector rotations
"""  
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

"""
-------------------------------------------------------
Beam section definition
"""
b = 10 #mm
h = 10 #mm
L = 100 #mm
J = (1/12)*b*(h**3) #mm**4
F = 10 #Force in Newtonws
E = 2.1 * 10**5 #Youngs modulus in MPa
"""
--------------------------------------------------------
Plot axis limits
"""
xLimit = [-5, L+10]
yLimit = [-0.02, 0.01]
"""
--------------------------------------------------------
"""

fig, ax = plt.subplots()
x = np.arange(0, L, 2)
line, = ax.plot(x,((-F*(x**3))/(3*E*J)))
x2 = [0, 0]
y2 = [yLimit[0]/2, yLimit[1]/2]
wall = ax.plot(x2,y2, color = 'red', linewidth=5)

def projection(_x, _F):
    """
    Due to bending, beams endpoint shifts to the fixed end direction.
    delta_L -- value of beam "shortening"
    """
    delta_L = ((1/40)*(_F**2)/((E**2)*(J**2))*(_x**5))
    return delta_L

def animate(_F):
    """
    Function calculates new data points of bending curve with regards to incrementing/increasing force.
    _F -- applied force
    Due to "shortening" of the beam, the x values needs to be updated as well.
    """
    line.set_ydata(((-_F*x**3)/(3*E*J)))  # update the Y data
    #update the X data 
    newX = []
    for elem in x:
        newX.append(elem-projection(elem, _F))
    line.set_xdata(newX)  
    return line,

def init():
    """
    Init function, sets limits on both axis, grid and other graph options.
    """
    ax.set_xlim(xLimit)
    ax.set_ylim(yLimit)
    ax.grid(True)
    ax.set_xlabel("Length [mm]")
    ax.set_ylabel("Deflection [mm]")
    #line.set_ydata([np.nan] * len(x))
    return line,

ani = animation.FuncAnimation(fig, animate, np.arange(1, F, 0.1), init_func=init,
                              interval=60, blit=True, save_count=50)

ani.save('animation.gif', writer='imagemagick', fps=60)

plt.show()

结果: Beam bending animation