首页 > 解决方案 > 为什么我的 plt.plot() 在绘制弹丸运动时不起作用?

问题描述

我正在绘制没有/有空气摩擦的弹丸运动,我现在在第一部分,这是没有空气摩擦的部分。

在我放好 plt.plot(x_nodrag,y_nodrag) 之后,它应该画出弹丸运动的曲线。但是由于某些原因,即使正在打印它们的数据,它也没有被绘制出来。我想知道为什么。

我知道这段代码可能有很多错误!!如果您愿意,请随时指出它们。谢谢你们的帮助。

这是图表的图片:https ://i.stack.imgur.com/znFLM.png

import numpy as np
import matplotlib.pyplot as plt

# Model parameters
M = 1.0          # Mass of projectile in kg
g = 9.8          # Acceleration due to gravity (m/s^2)
V = 80           # Initial velocity in m/s
ang = 60.0       # Angle of initial velocity in degree
Cd = 0.005       # Drag coefficient
dt = 0.5         # time step in s

# Set up the lists to store variables
# Start by putting the initial velocities at t=0
t = [0]                         # list to keep track of time
vx = [V*np.cos(ang/180*np.pi)]  # list for velocity x and y components
vy = [V*np.sin(ang/180*np.pi)]


#show the projectile motion without drag force
t1=0
vx_nodrag=V*np.cos(ang/180*np.pi) 
vy_nodrag=V*np.sin(ang/180*np.pi)

while (t1 < 100):
    x_nodrag=vx_nodrag*t1
    y_nodrag=vy_nodrag*t1+(0.5*-9.8*t1**2)
    plt.ylim([0,200])
    plt.xlim([0,270])
    plt.plot(x_nodrag,y_nodrag)
    print(x_nodrag,y_nodrag)
    t1=t1+dt

plt.show()

标签: pythonnumpymatplotlibphysics

解决方案


该命令plt.plot不适用于单个点。改为使用plt.scatter(x_nodrag, y_nodrag),或将连续坐标收集到列表或数组中,然后一次绘制整个图形。


推荐阅读