Matplotlib +セルロイドカメラが動的データを視覚化

インストール

pip install celluloid

helloworldコード

from celluloid import Camera
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax4 = plt.axes(projection='3d')
camera = Camera(fig)
t = np.linspace(0, 5 * np.pi, 128, endpoint=False)
for i in t2:
    ax4.plot(t, np.cos(t/2 + i), np.sin(t/2 +i), color='b')
    camera.snap()
    
animation = camera.animate()  
animation.save('celluloid_example.gif', writer = 'imagemagick')

ここに画像の説明を挿入

応用例

import math
import random
import numpy as np
from celluloid import Camera
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from math import *

def getlength(k):
    return np.exp(-20.0*(k-0.16))+6.0

def getspeed(l):
    return np.sqrt(l*5.0)-2.0
    
def recursive(control_points, T, B0=0, B1=0, dB0=0, dB1=0, ddB0=0, ddB1=0):
    if len(control_points) == 1:
        return control_points[0], -B0+(1-T)*dB0+B1+T*dB1, -2*dB0+(1-T)*ddB0+2*dB1+T*ddB1
    else:
        B0, dB0, ddB0 = recursive(control_points[0:-1], T)
        B1, dB1, ddB1 = recursive(control_points[1:], T)
        return (1-T)*B0 + T*B1 , -B0+(1-T)*dB0+B1+T*dB1, -2*dB0+(1-T)*ddB0+2*dB1+T*ddB1    
    
roadcpx = [1., 1., 20., 40., 60]
roadcpy = [1., 20., 20., 1., 60]

T = np.linspace(0, 1, 300)
x, dx, ddx = recursive(roadcpx, T)
y, dy, ddy = recursive(roadcpy, T)
theta = np.arctan2(dy, dx)
curvature = np.abs((dx*ddy-dy*ddx)/(dx**2+dy**2)**(3./2.))
def getArcLength(x, y):
    x0 = x[0]
    y0 = y[0]
    s = [0]
    for i in range(len(x)-1):
        x1 = x[i+1]
        y1 = y[i+1]
        dx = x1 - x0
        dy = y1 - y0
        ds = np.sqrt(dx*dx+dy*dy)
        s.append(ds+s[-1])
        x0 = x1
        y0 = y1
        
    return s

s = getArcLength(x,y)

def frenet_to_cartesian1D(rs, rx, ry, rtheta, s_condition, d_condition):
    if fabs(rs - s_condition[0])>= 1.0e-6:
        print("The reference point s and s_condition[0] don't match")
        
    cos_theta_r = cos(rtheta)
    sin_theta_r = sin(rtheta)
    
    x = rx - sin_theta_r * d_condition[0]
    y = ry + cos_theta_r * d_condition[0]    
    return x, y

left_bound = []
right_bound = []

for i in range(300):
    rs = s[i]
    rx = x[i]
    ry = y[i]
    
    rtheta = theta[i]
    
    l_s_condition = np.array([rs])
    l_d_condition = np.array([1.5])
    lx, ly = frenet_to_cartesian1D(rs, rx, ry, rtheta, l_s_condition, l_d_condition)
    left_bound.append(np.array([lx, ly]))
    
    r_s_condition = np.array([rs])
    r_d_condition = np.array([-1.5])
    rx, ry = frenet_to_cartesian1D(rs, rx, ry, rtheta, r_s_condition, r_d_condition)   
    right_bound.append(np.array([rx, ry]))
left_bound = np.array(left_bound)
right_bound = np.array(right_bound)
fig = plt.figure(figsize=(8, 8),
                 facecolor='lightyellow'
                )
camera = Camera(fig)
for i in range(300): 
    plt.plot(x, y, '--y')
    plt.plot(left_bound[:,0],left_bound[:,1], 'b')
    plt.plot(right_bound[:,0],right_bound[:,1], 'b')    
    plt.scatter(x[i], y[i], marker='o', color='g')
    length = round(getlength(curvature[i]), 2)
    speed = round(getspeed(length), 2)
    for j in range(i, 300):
        if s[j] > s[i]+length:
            plt.plot(x[i:j], y[i:j], 'r')
            break
    plt.text(1, 56, 'trajectory length: '+str(length)+'m', fontsize=16)
    plt.text(1, 52, 'desired speed: '+str(speed)+'m/s', fontsize=16)
    plt.text(1, 60, 'lane curvature: '+str(curvature[i]), fontsize=16)
    
#     plt.show()
    camera.snap()   

    
    
animation = camera.animate()  
animation.save('celluloid_example.gif', writer = 'imagemagick')

ここに画像の説明を挿入

おすすめ

転載: blog.csdn.net/u013468614/article/details/110090828