如何在Matplotlib中使用单个绘图命令绘制多色曲线 [英] How to plot a multicolored curve using a single plot command in matplotlib

查看:67
本文介绍了如何在Matplotlib中使用单个绘图命令绘制多色曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我理解正确,则该功能:

If I understand correctly, the function:

matplotlib.pyplot.plot(x, y)

绘制 len(x)-1 条单独的线段 - 一条从 (x[0], y[0]) 到 (x[1],y[1]),一条从 (x[1],y [1])到(x [2],y [2])等.在我的应用程序中,我想显示一条曲线,该曲线由以这种方式连接数据点的一系列线段组成,但是有一个额外的部分与每个数据点之间的过渡相关的数据 (z),我想用线段的颜色表示.显然,这样做的一种方法如下:

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. 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])

但是有没有一种方法,而不必为每个线段单独调用matplotlib.pyplot.plot?

but is there a way to do it that doesn't involve a separate call to matplotlib.pyplot.plot for each line segment?

推荐答案

如果实现了颜色栏,则类似于颜色比例.

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.

这篇关于如何在Matplotlib中使用单个绘图命令绘制多色曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆