在Python中绘制图表
我是 Python 新手,正在尝试根据 此处 找到的 pyODE 教程绘制图表。我使用 pylab 进行绘图。 下面是代码的主要部分,#added
代表我为了尝试显示图表而添加的代码。查看值本身时,y
和 v
会发生变化,x,z,u,w
仍为 0.000< /代码>。当我运行程序时,轴刻度不断缩放,这意味着值发生了一些变化,但没有显示任何线条。我做错了什么?
谢谢
yplot = 0 #added
#do the simulation
total_time = 0.0
dt = 0.04
while total_time<2.0:
x,y,z = body.getPosition()
u,v,w = body.getLinearVel()
print "%1.2fsec: pos=(%6.3f,%6.3f,%6.3f) vel=(%6.3f,%6.3f,%6.3f)" % \
(total_time, x,y,z,u,v,w)
world.step(dt)
total_time += dt
yplot += y #added
plot(total_time, yplot) #added
xlabel('Time') #added
ylabel('Height') #added
show() #added
I'm new to Python am trying to plot a graph based on the pyODE tutorial found here. I'm using pylab
for the plotting.
Below is the main part of the code and #added
represents the code I've added in order to try and display the graph. When looking at the values themselves, y
and v
are the ones that change and x,z,u,w
remain 0.000
. When I run the program, the axis scale keeps scaling, implying that something is happening regarding the values, but no line is displayed. What am I doing wrong?
Thanks
yplot = 0 #added
#do the simulation
total_time = 0.0
dt = 0.04
while total_time<2.0:
x,y,z = body.getPosition()
u,v,w = body.getLinearVel()
print "%1.2fsec: pos=(%6.3f,%6.3f,%6.3f) vel=(%6.3f,%6.3f,%6.3f)" % \
(total_time, x,y,z,u,v,w)
world.step(dt)
total_time += dt
yplot += y #added
plot(total_time, yplot) #added
xlabel('Time') #added
ylabel('Height') #added
show() #added
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
诀窍是首先累积要绘制的所有值,然后只需调用一次
plot
即可。plot 的第三个参数
'r-'
告诉pylab
绘制一条连接times
,yvals 中列出的点的红线
。当您一次绘制一个点时,无法告诉 pylab 连接这些点,因为每个图仅包含一个点。对每个点调用plot
也是非常低效的。The trick is to accumulate all the values you want to plot first, and then just call
plot
once.The third argument to plot,
'r-'
, tellspylab
to draw a red line connecting the points listed intimes
,yvals
. When you plot points one-at-a-time, there is no way to tellpylab
to connect the dots because each plot contains only a single point. Callingplot
for each point is also highly inefficient.