1

I'm new to Matplotlib. I have the position of a person every second, and I am trying to make a graph showing this. I have managed to show it, but now I would like it to display different colors according to their speed. So, I need the plt.plot() color to depend on the distance between each pair of points, instead of being always the same. This is what I have right now:

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()

I would like to add some variable that defines the color according to distance([x[i],y[i]],[x[j],y[j]])

Does anyone know how to do this?

Thanks!

2
  • 1
    Add a piece of code and we'll be able to help you out! Commented Jul 18, 2012 at 21:31
  • What he needs is a countour lines graphic that asign the same color to the set of position vectores that have the same velocity Commented Jul 18, 2012 at 21:56

3 Answers 3

1

I have written some code to show how I would go about solving this issue. To my knowledge, there is no way of colouring each line segment, therefore I have had to loop over each step, plotting each time (and picking the appropriate colour).

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()

Code output

Hopefully the code and comments are sufficiently self documenting (the mappable part to create the colorbar is perhaps the hardest, and most tricky part, and you may not even want one!)

Sign up to request clarification or add additional context in comments.

Comments

1

scatter will do what you want (doc).

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

However, this will not connect the markers. If you want a line that is a different color on each segment I think you will have to plot a large number of two point lines.

EDIT: added plot as suggested in comments by Vorticity

1 Comment

I think this is a good answer. To add line segments between the points, simply call plt.plot(x,y) along with plt.scatter(), but once per person.
0

You can also try quiver. It makes a direction field (arrows) plot.

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()

enter image description here

If you want, you can also make the arrow size dependent on the speed at that position.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.