1

私はMatplotlibが初めてです。私は毎秒人の位置を持っており、これを示すグラフを作成しようとしています。なんとか表示できましたが、今度は速度に応じて色を変えて表示したいと思います。そのため、常に同じではなく、各ポイントのペア間の距離に依存する plt.plot() の色が必要です。これは私が今持っているものです:

x = [i[0] for i in walk]
y = [i[1] for i in walk]
plt.clf()
fig = plt.gcf()
plt.axis([0, 391, 0, 578])
im = plt.imread('field.png')
cancha = plt.imshow(im)
plt.plot(x,y)
plt.axis('off')
plt.savefig( IMG_DIR + 'match.png',bbox_inches='tight')
plt.clf()

distance([x[i],y[i]],[x[j],y[j]]) に従って色を定義する変数を追加したいと思います

誰もこれを行う方法を知っていますか?

ありがとう!

4

3 に答える 3

1

scatterあなたが望むことをします(doc)。

 plt.scatter(x,y,c=distance(x,y))
 plt.plot(x,y,'-') # adds lines between points

ただし、これではマーカーが接続されません。各セグメントで異なる色の線が必要な場合は、多数の 2 点線をプロットする必要があると思います。

編集:plotVorticityによるコメントで提案されているように追加

于 2012-07-18T21:51:10.293 に答える
1

この問題を解決する方法を示すコードをいくつか書きました。私の知る限り、各線分に色を付ける方法はないため、各ステップをループして、毎回プロットする (そして適切な色を選択する) 必要がありました。

import matplotlib.pyplot as plt
import numpy

x = numpy.array([1, 1.5, 5, 1, 4, 4])
y = numpy.array([1, 2, 1, 3, 5, 5])

# calculate the absolute distance for each step
distances = numpy.abs(numpy.diff((x**2 + y**2)**0.5))

ax = plt.axes()

# pick a colormap, and define a normalization to take distances to the range 0-1
cmap = plt.get_cmap('jet')
norm = plt.normalize(min(distances), max(distances))

# loop through each walk segment, plotting the line as coloured by
# the distance of the segment, scaled with the norm and a colour chosen
# using the normed distance and the cmap
for i in range(1, len(x)):
    distance = distances[i-1]
    x0, y0 = x[i-1], y[i-1]
    x1, y1 = x[i], y[i]
    ax.plot([x0, x1], [y0, y1], '-', color=cmap(norm(distance)))

# put points for each observation (no colouring)
ax.scatter(x, y)

# create a mappable suitable for creation of a colorbar
import matplotlib.cm as cm
mappable = cm.ScalarMappable(norm, cmap)
mappable.set_array(distance)

# create the colorbar
cb = plt.colorbar(mappable)    
cb.set_label('Distance / meters')

# add some additional information
plt.title("Person 1's walk path")
plt.xlabel('x / meters')
plt.ylabel('y / meters')

# add some additional text to show the total distance walked. 
# The coordinates are in axes coordinates (ax.transAxes).
plt.text(0.99, 0.01, 'Total distance: %.02f meters' % numpy.sum(distances), 
         transform=ax.transAxes, horizontalalignment='right')

plt.show()

コード出力

コードとコメントが十分に自己文書化されていることを願っています (カラーバーを作成するためのマッピング可能な部分は、おそらく最も難しく、最もトリッキーな部分であり、必要ではないかもしれません!)

于 2012-07-19T08:43:03.910 に答える
0

試すこともできますquiver。方向フィールド(矢印)プロットを作成します。

import pylab as plt

x=[12, 13, 14, 15, 16]
y=[14, 15, 16, 17, 18]
speed=[1,2,3,4,5]

# Determine the direction by the difference between consecutive points
v_x=[j-i for i, j in zip(x[:-1], x[1:])] 
v_x.append(v_x[-1]) # The last point 
v_y=[j-i for i, j in zip(y[:-1], y[1:])]
v_y.append(v_y[-1]) # The last point

plt.quiver(x,y,v_x,v_y,speed)
plt.colorbar()
plt.xlim(11,17)
plt.ylim(13,19)
plt.show()

ここに画像の説明を入力してください

必要に応じて、矢印のサイズをその位置での速度に依存させることもできます。

于 2012-07-19T08:45:56.667 に答える