【问题标题】:Update colour of plot in 3d matplotlib更新 3d matplotlib 中绘图的颜色
【发布时间】:2015-10-02 22:46:36
【问题描述】:

我想创建一个 3D 动画散点图,其中最新的点以某种颜色显示,然后变灰,直到生成下一个点。

输入文件 'coordinates.txt' 仅包含三列中的 x、y 和 z 值。

只要我跳过 z 值并只使用 2d 投影,代码就可以工作。当我添加 z 坐标时,我得到一个 TypeError: object of type 'float' has no len()

我不明白为什么我会在 3d 中收到此错误,但在 2d 投影中却没有,以及为什么它要在 3d 版本中计算 len()。

如果有人能帮我解决这个问题,我会很高兴的!

input_file = open('coordinates.txt', 'r').readlines()

fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')

pylab.xlim([0,6])
pylab.ylim([0,6])
ax.set_zlim([0,6])


line = None

def main(i):
    global line
    if line is not None: 
        line.set_color('gray')
    line, = ax.plot(x,y,z, 'ro')

for row in input_file: 
    el = row.split()
    x = float(el[0])
    y = float(el[1])
    z = float(el[2])

    plt.ion()
    plt.show()
    ani = animation.FuncAnimation(fig, main)
    plt.draw()

这里是整个回溯:

回溯(最近一次通话最后一次):
文件“./def_func_3d.py”,第 65 行,在
ani = animation.FuncAnimation(fig, main)
init
中的文件“/usr/lib/pymodules/python2.7/matplotlib/animation.py”,第 1010 行 TimedAnimation.init(self, fig, **kwargs)
init
中的文件“/usr/lib/pymodules/python2.7/matplotlib/animation.py”,第 864 行 *args, **kwargs)
init
中的文件“/usr/lib/pymodules/python2.7/matplotlib/animation.py”,第 545 行 self._init_draw()
文件“/usr/lib/pymodules/python2.7/matplotlib/animation.py”,第 1035 行,在 _init_draw
self._draw_frame(下一个(self.new_frame_seq()))
文件“/usr/lib/pymodules/python2.7/matplotlib/animation.py”,第 1049 行,在 _draw_frame
self._drawn_artists = self._func(framedata, *self._args)
文件“./def_func_3d.py”,第 43 行,在 main
线,= ax.plot(x,y,z, 'ro')
文件“/usr/lib/pymodules/python2.7/mpl_toolkits/mplot3d/axes3d.py”,第 1511 行,在 plot
zs = np.ones(len(xs)) * zs
TypeError: 'float' 类型的对象没有 len()

【问题讨论】:

  • 能否包含整个错误回溯?查看错误发生的位置会有所帮助。
  • 谢谢,我在帖子中添加了整个回溯。

标签: matplotlib 3d


【解决方案1】:

plot 期待 xy 的可迭代对象,如果它是标量,它会使用它来调整 z 的大小。把代码改成应该没问题

line, = ax.plot([x], [y], z, 'ro')

【讨论】:

    猜你喜欢
    • 2017-03-17
    • 2013-07-19
    • 1970-01-01
    • 1970-01-01
    • 2018-12-06
    • 1970-01-01
    • 2013-03-26
    • 2018-08-12
    • 2016-12-30
    相关资源
    最近更新 更多