【发布时间】:2021-10-08 12:43:00
【问题描述】:
我是 Python 新手。出于某种原因,当我查看绘图时,它显示所有数据,好像Y = 0 但最后一个数据,这很奇怪,因为当我要求它打印Y 它显示正确的值。我做错了什么?
import math
import numpy as np
import matplotlib.pyplot as plt
y0=2 # [m]
g=9.81 # [m/s^2]
v=20 # initial speed [m/s]
y_target=1 # [m]
x=35 # [m]
n_iter=50
theta=np.linspace(0,0.5*math.pi,n_iter) # theta input [rad]
Y=np.zeros(n_iter) # y output [m]
for i in range(n_iter):
Y[i]=math.tan(theta[i])*x-g/(2*(v*math.cos(theta[i]))**2)*x**2+y0
plt.plot(theta,Y)
plt.ylabel('y [m]')
plt.xlabel('theta [rad]')
plt.ylim(top=max(Y),bottom=min(Y))
plt.show()
【问题讨论】:
-
它没有设置 Y=0。最后一个值是更大的负值,尝试
plt.plot(theta[:-2],Y[:-2])(不设置plt.ylim)以更详细地查看您的情节。 -
设置
plt.yscale('symlog'):Code and Plot
标签: python numpy matplotlib