3

我正在尝试绘制随时间演变的 3D 线轨迹,并且我希望改变颜色以显示时间的流逝(例如从浅蓝色到深蓝色)。但是,明显缺乏使用 matplotlib 的教程Line3DCollection这是我能找到的最接近的,但我得到的只是一条白线。

这是我的代码。

import matplotlib.pyplot as plot
from mpl_toolkits.mplot3d.axes3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Line3DCollection
import numpy as np

# X has shape (3, n)
c = np.linspace(0, 1., num = X.shape[1])[::-1]
a = np.ones(shape = c.shape[0])
r = zip(a, c, c, a) # an attempt to make red vary from light to dark

# r, which contains n tuples of the form (r,g,b,a), looks something like this:
# [(1.0, 1.0, 1.0, 1.0), 
# (1.0, 0.99998283232330165, 0.99998283232330165, 1.0),
# (1.0, 0.9999656646466033, 0.9999656646466033, 1.0),
# (1.0, 0.99994849696990495, 0.99994849696990495, 1.0),
# ..., 
# (1.0, 1.7167676698312416e-05, 1.7167676698312416e-05, 1.0),
# (1.0, 0.0, 0.0, 1.0)]

fig = plot.figure()
ax = fig.gca(projection = '3d')

points = np.array([X[0], X[1], X[2]]).T.reshape(-1, 1, 3)
segs = np.concatenate([points[:-1], points[1:]], axis = 1)
lc = Line3DCollection(segs, colors = r)
ax.add_collection3d(lc)

ax.set_xlim(-0.45, 0.45)
ax.set_ylim(-0.4, 0.5)
ax.set_zlim(-0.45, 0.45)

plot.show()

但是,这就是我得到的:

3d 时间颜色变化图

只是一堆白色的线段,颜色没有变化。我究竟做错了什么?谢谢!

4

2 回答 2

6

您的代码工作得很好,这里有一些示例。基本上,这是您的带有自定义 X 集的代码。

fig = plot.figure();
ax = fig.gca(projection = '3d')
X = [(0,0,0,1,0),(0,0,1,0,0),(0,1,0,0,0)]
points = np.array([X[0], X[1], X[2]]).T.reshape(-1, 1, 3)
r = [(1.0, 1.0, 1.0, 1.0), (1.0, 0.75, 0.75, 1.0), (1.0, 0.5, 0.5, 1.0), (1.0, 0.25, 0.25, 1.0), (1.0, 0.0, 0.0, 1.0)];

segs = np.concatenate([points[:-1], points[1:]], axis = 1)
ax.add_collection(Line3DCollection(segs,colors=list(r)))

plot.show()

情节是这样的:

在此处输入图像描述

于 2014-02-27T20:36:20.367 回答
1

哇,所以事实证明问题X实际上不是形状(3, n),而是类似的东西(3, n^10),但我只是在绘制n点,因此颜色似乎永远不会改变(为什么r似乎有极小的间隔......有些东西就像我只绘制 250 点时的 58,000 点)。

所以是的,这是一个错误。对于那个很抱歉; 现在工作正常。

于 2014-02-27T20:20:47.180 回答