【问题标题】:Matplotlib interactive environment does not workMatplotlib 交互环境不起作用
【发布时间】:2013-10-28 18:10:05
【问题描述】:

我在 OS X 上使用 Encopy(由 Enthought Inc. 提供)来编写我的 Python 程序。我从here 获取的以下代码仅生成一个点然后终止:

from pylab import *
import time
t = linspace(0.0, pi, 100)
x = cos(t)
y = sin(t)

ion()  # turn on interactive mode
figure(0)
subplot(111, autoscale_on=False, xlim=(-1.2, 1.2), ylim=(-.2, 1.2))

point = plot([x[0]], [y[0]], marker='o', mfc='r', ms=3)

for j in arange(len(t)):
    # reset x/y-data of point
    setp(point[0], data=(x[j], y[j]))
    time.sleep(0.05)
    draw() # redraw current figure

ioff() # turn off interactive mode
show()

任何想法可能是什么问题? 下面是我得到的结果的照片。

【问题讨论】:

  • 使用plt.pause 而不是sleep
  • @tcaswell,谢谢。但仍然是一个点!

标签: python matplotlib enthought


【解决方案1】:

它只绘制一个点,因为您只是告诉它绘制一个点。如果你想画线到 j 使用以下:

from pylab import *

t = linspace(0.0, pi, 100)
x = cos(t)
y = sin(t)
figure(0)
subplot(111, autoscale_on=False, xlim=(-1.2, 1.2), ylim=(-.2, 1.2))

point,  = plot([x[0]], [y[0]], marker='o', mfc='r', ms=3)

for j in arange(len(t)):
    # reset x/y-data of point
    point.set_data(x[:j], y[:j])
    plt.pause(0.05)
    plt.draw() # redraw current figure

【讨论】:

  • 非常感谢,但是您做了哪些改变来改变现状?我不太明白!
  • @Naji 我建议您手动浏览代码并查看每个函数的作用。
  • 我可以看到您更改了 point.set_data 参数。好吧,根据 matplotlib 的教程, setp 用于设置线条属性,我看不出该代码有什么问题。另外,您的代码还有另一件有趣的事情,您甚至不需要更改交互模式。怎么来的??
猜你喜欢
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 2021-06-01
  • 2018-01-21
  • 2012-07-10
  • 2020-01-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多