在Python中绘制图表

发布于 2024-10-09 18:18:07 字数 801 浏览 2 评论 0原文

我是 Python 新手,正在尝试根据 此处 找到的 pyODE 教程绘制图表。我使用 pylab 进行绘图。 下面是代码的主要部分,#added 代表我为了尝试显示图表而添加的代码。查看值本身时,yv 会发生变化,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失退 2024-10-16 18:18:07

诀窍是首先累积要绘制的所有值,然后只需调用一次 plot 即可。

yplot = 0 #added

#do the simulation
total_time = 0.0
dt = 0.04
times=[]
yvals=[]
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 
    times.append(total_time)
    yvals.append(yplot)
plot(times, yvals,'r-')
xlabel('Time') #added
ylabel('Height') #added
show() #added

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.

yplot = 0 #added

#do the simulation
total_time = 0.0
dt = 0.04
times=[]
yvals=[]
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 
    times.append(total_time)
    yvals.append(yplot)
plot(times, yvals,'r-')
xlabel('Time') #added
ylabel('Height') #added
show() #added

The third argument to plot, 'r-', tells pylab to draw a red line connecting the points listed in times,yvals. When you plot points one-at-a-time, there is no way to tell pylab to connect the dots because each plot contains only a single point. Calling plot for each point is also highly inefficient.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文