時々の色のための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()
.
とプロットは次のようになります。
他のヒント
WOW、問題が発生したことが判明したことがわかりました。そして、なぜX
が非常に小さい間隔を持つように思われます...私がプロットしていたときに58,000ポイントのようなものがありました250)。
そうはい、それはバグでした。それについてすみません。今は元気です。
所属していません StackOverflow