[Scientific / technical calculation by Python] Drawing animation of parabolic motion with locus, matplotlib

Introduction

Using matplotlib's ArtistAnimation method The motion of an object performing a parabolic motion is displayed with a trajectory. Here, ** a method of displaying two pictures of the locus part and the moving part of the object in an overlapping manner ** was used. ** It's a simple and easy way for everyone to think, but it may be useful when you want a quick visualization. .. ** **


Contents

** It is a vertical throwing problem in the direction of the uniform gravitational field in the direction of $ y $ $ \ theta $. ** **

Assuming that the position of the mass point is $ (x_0, y_0) $ and the magnitude of the initial velocity is $ V_0 $ when the time $ t = 0 $, the mass points $ x (t) $ and $ y (t) at the time $ t $ ) $ Coordinates are as follows. This mass movement draws a parabola. x(t) = x_0 + V_0 \ cos(\theta) y(t)= y_0 + V_0 \ sin(\theta)-0.5\ \ g\ t^2

In this problem, set $ V_0 = 100 $ m / s, $ (x_0, y_0) = (0, 0) $, $ \ theta = \ pi / 4 $ (= 45 degrees) to draw the motion of the mass point. ..


code

locus.py


"""
Animated parabolic movement with a trajectory
Animation with a locus
"""

import matplotlib.pyplot as plt
%matplotlib nbagg  # Jupyter-Add when displaying animation in notebook
from matplotlib.animation import ArtistAnimation #Import methods for creating animations
import numpy as np

fig = plt.figure()

anim = [] #A list for storing the data of the para-para diagram drawn for animation
tt = np.arange(0,15,0.5) #Time setting for drawing: t=0 from 0 to 15.In 5 increments.

x_all=[] #List for storing data at all x positions
y_all=[] #List for storing data at all y positions

#Setting initial conditions
V0 =100 #The magnitude of the initial velocity: 100 m/s
theta=np.pi/4
x0=0 #Initial position: x=0
y0=0 #Initial position: y = 0
g=9.8 #Gravitational constant[m/s^2]

for t in tt:
    x= [V0*np.cos(theta)*t+x0]  # x(t)Description of
    y = [-( g/2)*t**2+V0*np.sin(theta)*t+y0] # y(t)Description of
    x_all.append(x[0]) #Stores data every moment of x
    y_all.append(y[0]) #Store data every moment of y

    #Create two pictures, the mass point at time t and the locus of motion up to time t, and store them in the animation list.
    im=plt.plot(x,y,'o', x_all,y_all, '--', color='red',markersize=10, linewidth = 2, aa=True)
    anim.append(im)


anim = ArtistAnimation(fig, anim) #Animation creation

#Draw customization
plt.xlabel('X',fontsize=18) # 
plt.ylabel('Y',fontsize=18)
plt.xlim(0, 1100)
plt.ylim(-10,300)
plt.hlines([0], 0, 2000, linestyles="-")  # y=Draw a line at 0.

fig.show() 
anim.save("t.gif", writer='imagemagick')   #Animation.Save as gif and create a gif animation file.

result

t.gif

An animation of the movement of a mass point when thrown at an initial velocity of 100 m / s in an oblique direction of 45 degrees is displayed with a trajectory.


Correction / change log:

August 3, 2017: ** Removed mass m in y (t) **. T_Shinaji, thank you for pointing out!

August 10, 2017: Changed from anim.save ("t.gif") to ** anim.save ("t.gif", writer ='imagemagick') **. Thank you to yoddy for pointing out!


References

For Artist Animation, chez_sugi's Animation with matplotlib I was allowed to refer to.

Recommended Posts

[Scientific / technical calculation by Python] Drawing animation of parabolic motion with locus, matplotlib
[Scientific / technical calculation by Python] Drawing of 3D curved surface, surface, wireframe, visualization, matplotlib
[Scientific / technical calculation by Python] Drawing, visualization, matplotlib of 2D (color) contour lines, etc.
[Scientific / technical calculation by Python] Histogram, visualization, matplotlib
[Scientific / technical calculation by Python] Logarithmic graph, visualization, matplotlib
[Scientific / technical calculation by Python] Polar graph, visualization, matplotlib
[Scientific / technical calculation by Python] Plot, visualize, matplotlib 2D data with error bars
[Scientific / technical calculation by Python] Basic operation of arrays, numpy
[Scientific / technical calculation by Python] Plot, visualization, matplotlib of 2D data read from file
[Scientific / technical calculation by Python] Sum calculation, numerical calculation
[Scientific / technical calculation by Python] Vector field visualization example, electrostatic field, matplotlib
[Scientific and technical calculation by Python] Drawing of fractal figures [Sierpinski triangle, Bernsley fern, fractal tree]
Scientific / technical calculation by Python] Drawing and visualization of 3D isosurface and its cross section using mayavi
[Scientific / technical calculation by Python] Fitting by nonlinear function, equation of state, scipy
[Scientific / technical calculation by Python] Calculation of matrix product by @ operator, python3.5 or later, numpy
[Scientific / technical calculation by Python] Inverse matrix calculation, numpy
[Scientific / technical calculation by Python] Lagrange interpolation, numerical calculation
[Scientific / technical calculation by Python] Numerical calculation to find the value of derivative (differential)
[Scientific / technical calculation by Python] Analytical solution to find the solution of equation sympy
[Scientific / technical calculation by Python] Monte Carlo integration, numerical calculation, numpy
[Scientific / technical calculation by Python] Numerical solution of second-order ordinary differential equations, initial value problem, numerical calculation
[Scientific / technical calculation by Python] List of matrices that appear in Hinpan in numerical linear algebra
[Scientific / technical calculation by Python] List of usage of (special) functions used in physics by using scipy
[Scientific / technical calculation by Python] Numerical solution of one-dimensional harmonic oscillator problem by velocity Verlet method
[Scientific / technical calculation by Python] Numerical solution of eigenvalue problem of matrix by power method, numerical linear algebra
Create plot animation with Python + Matplotlib
[Scientific / technical calculation by Python] Solving simultaneous linear equations, numerical calculation, numpy
[Scientific / technical calculation by Python] 1D-3D discrete fast Fourier transform, scipy
[Scientific / technical calculation by Python] 2D random walk (drunk walk problem), numerical calculation
[Scientific / technical calculation by Python] Monte Carlo simulation of thermodynamics of 2D Ising spin system by Metropolis method
[Scientific / technical calculation by Python] Derivation of analytical solutions for quadratic and cubic equations, mathematical formulas, sympy
[Scientific / technical calculation by Python] Numerical solution of one-dimensional unsteady heat conduction equation by Crank-Nicholson method (implicit method) and FTCS method (positive solution method), parabolic partial differential equation
1. Statistics learned with Python 1-3. Calculation of various statistics (statistics)
Drawing with Matrix-Reinventor of Python Image Processing-
[Scientific / technical calculation by Python] Solving ordinary differential equations, mathematical formulas, sympy
[Scientific / technical calculation by Python] Solving the boundary value problem of ordinary differential equations in matrix format, numerical calculation
[Scientific / technical calculation by Python] Solving (generalized) eigenvalue problem using numpy / scipy, using library
[Scientific / technical calculation by Python] Solving second-order ordinary differential equations by Numerov method, numerical calculation
[Scientific / technical calculation by Python] Solving one-dimensional Newton equation by the 4th-order Runge-Kutta method
[Python] limit axis of 3D graph with Matplotlib
1. Statistics learned with Python 1-2. Calculation of various statistics (Numpy)
Calculation of technical indicators by TA-Lib and pandas
The basis of graph theory with matplotlib animation
[Python] Customize Colormap when drawing graphs with matplotlib
[Scientific / technical calculation by Python] Numerical solution of one-dimensional and two-dimensional wave equations by FTCS method (explicit method), hyperbolic partial differential equations
Animation with matplotlib
Animation with matplotlib
[Scientific / technical calculation by Python] Generation of non-uniform random numbers giving a given probability density function, Monte Carlo simulation
Memo of "Cython-Speeding up Python by fusing with C"
[Science / technical calculation by Python] Numerical solution of first-order ordinary differential equations, initial value problem, numerical calculation
[Scientific / technical calculation by Python] Wave "beat" and group velocity, wave superposition, visualization, high school physics
Heatmap with Python + matplotlib
Real-time drawing with matplotlib
Drawing with Python Tinker
Installation of matplotlib (Python 3.3.2)
Numerical calculation with Python
Perform isocurrent analysis of open channels with Python and matplotlib
Mass generation of QR code with character display by Python
[Introduction to Data Scientists] Basics of scientific calculation, data processing, and how to use graph drawing library ♬ Basics of Matplotlib
[Scientific / technical calculation by Python] Numerical solution of two-dimensional Laplace-Poisson equation by Jacobi method for electrostatic potential, elliptic partial differential equation, boundary value problem