[英]How to plot a multicolored curve using a single plot command in matplotlib
If I understand correctly, the function: 如果我理解正确,功能:
matplotlib.pyplot.plot(x, y)
plots len(x)-1 separate line segments - one going from (x[0], y[0]) to (x[1],y[1]), one going from (x[1],y[1]) to (x[2], y[2]), etc. In my application, I want to display a curve consisting of a series of line segments connecting data points in this way, but there is an extra piece of data (z) associated with the transition between each of these data points, that I want to represent by the color of the line segment. plots len(x)-1个单独的线段 - 一个从(x [0],y [0])到(x [1],y [1]),一个来自(x [1],y [1] ])到(x [2],y [2])等。在我的应用程序中,我想显示一条由一系列以这种方式连接数据点的线段组成的曲线,但是还有一段额外的数据( z)与每个数据点之间的过渡相关联,我想通过线段的颜色来表示。 Clearly one way of doing this is the following:
显然,这样做的一种方法如下:
for i in range(len(x)-1)):
matplotlib.pyplot.plot(x[i:i+2],y[i:i+2], color=z[i])
but is there a way to do it that doesn't involve a separate call to matplotlib.pyplot.plot for each line segment? 但有没有办法做到这一点,不涉及对每个线段单独调用matplotlib.pyplot.plot?
您应该能够使用matplotlib.collections.LineCollection
接受colors
参数(RGBA元组的序列)。
If you implement a colorbar, something similar to this , then you should be able to do it in one line. 如果你实现了一个类似于此的颜色条 ,那么你应该能够在一行中完成它。 The trick may be to pick a colorscale that changes on the order of your line segments.
诀窍可能是选择一个按照线段顺序改变的色阶 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.