15

2 つの配列のデータからプロットされた線の色を変えようとしています (例: ax.plot(x,y))。xインデックスがおよびy増加するにつれて、色が変化するはずです。私は本質的に、配列内のデータの自然な「時間」パラメータ化をキャプチャしようとしていxますy.

完璧な世界では、次のようなものが必要です。

fig = pyplot.figure()
ax  = fig.add_subplot(111)
x   = myXdata 
y   = myYdata

# length of x and y is 100
ax.plot(x,y,color=[i/100,0,0]) # where i is the index into x (and y)

黒から濃い赤、さらには明るい赤まで変化する色の線を作成します。

「時間」配列によって明示的にパラメーター化された関数をプロットするのにうまく機能するを見てきましたが、生データで機能させることはできません...

4

1 に答える 1

21

2番目の例はあなたが望むものです...私はあなたの例に合うように編集しましたが、もっと重要なことは、何が起こっているのかを理解するために私のコメントを読んでください:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib.collections import LineCollection

x   = myXdata 
y   = myYdata
t = np.linspace(0,1,x.shape[0]) # your "time" variable

# set up a list of (x,y) points
points = np.array([x,y]).transpose().reshape(-1,1,2)
print points.shape  # Out: (len(x),1,2)

# set up a list of segments
segs = np.concatenate([points[:-1],points[1:]],axis=1)
print segs.shape  # Out: ( len(x)-1, 2, 2 )
                  # see what we've done here -- we've mapped our (x,y)
                  # points to an array of segment start/end coordinates.
                  # segs[i,0,:] == segs[i-1,1,:]

# make the collection of segments
lc = LineCollection(segs, cmap=plt.get_cmap('jet'))
lc.set_array(t) # color the segments by our parameter

# plot the collection
plt.gca().add_collection(lc) # add the collection to the plot
plt.xlim(x.min(), x.max()) # line collections don't auto-scale the plot
plt.ylim(y.min(), y.max())
于 2012-04-20T20:32:53.870 に答える