【问题标题】:How to plot in different colors in Matplotlib如何在 Matplotlib 中绘制不同的颜色
【发布时间】:2012-07-18 21:29:59
【问题描述】:

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

我想添加一些根据距离定义颜色的变量([x[i],y[i]],[x[j],y[j]])

有人知道怎么做吗?

谢谢!

【问题讨论】:

  • 添加一段代码,我们将能够为您提供帮助!
  • 他需要的是一个计数线图形,为具有相同速度的位置矢量集分配相同的颜色

标签: python graph matplotlib


【解决方案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()

希望代码和 cmets 具有足够的自我记录能力(创建颜色条的可映射部分可能是最难、最棘手的部分,您甚至可能不想要一个!)

【讨论】:

    【解决方案2】:

    scatter 会做你想做的事 (doc)。

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

    但是,这不会连接标记。如果您想要在每个线段上使用不同颜色的线,我认为您将不得不绘制大量的两条点线。

    编辑:按照 Vorticity 在 cmets 中的建议添加了 plot

    【讨论】:

    • 我认为这是一个很好的答案。要在点之间添加线段,只需调用 plt.plot(x,y) 和 plt.scatter(),但每人一次。
    【解决方案3】:

    你也可以试试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()
    

    如果你愿意,你也可以让箭头的大小取决于那个位置的速度。

    【讨论】:

      猜你喜欢
      • 2013-04-07
      • 1970-01-01
      • 2013-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-19
      • 2020-07-22
      相关资源
      最近更新 更多