matplotlib line3dcollection用于时变颜色
-
24-12-2019 - |
题
我试图绘制随着时间的推移发展的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()
.
但是,这是我得到的:
解决方案
你的代码很好,这里有点样本。基本上,这是您的代码,具有自定义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()
.
和绘图如下所示:
其他提示
哇,事实证明了这个问题是生成的是形状生成的不是的形状生成的,而是像生成的东西,但我只是绘制了X
点,因此颜色似乎永远不会改变(为什么生成的间隔似乎具有极小的间隔......当我绘制的时候只有58,000点,我绘制了250)。
所以是的,这是一个错误。对于那个很抱歉;它现在正常工作。
不隶属于 StackOverflow