matplotlib中的线图颜色随线索引的数据索引而变化吗? [英] Having line color vary with data index for line graph in matplotlib?

查看:260
本文介绍了matplotlib中的线图颜色随线索引的数据索引而变化吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个2D数据数组,它们在同一根轴上生成许多时间序列的图.目前,每行的颜色只是循环显示而已,并不代表任何意义.

So I have a 2D array of data producing a plot of many timeseries on the same axes. At the moment, the colour of each line just cycles through and doesn't mean anything.

我想以某种方式将每行的颜色映射到其数据的索引-因此索引较低的一组数据显示为红色,然后在索引较高的情况下变为蓝色.

I want to somehow map the colour of each line to the index of its data - so a set of data with a low index appears red and then fades to blue at a high index.

为澄清起见,每条线在整个过程中应使用相同的颜色,而不会随着时间而褪色.每行之间应该有区别.

To clarify, each individual line should be the same colour throughout, not fading with time. The difference should be between each line.

谢谢!

推荐答案

通常,您将颜色图传递给绘图函数,但也可以将数字或数组传递给颜色图并获取颜色.

Often you pass a colormap to a plotting function, but you can also pass a number or array to a colormap and get the colors in return.

因此要根据变量为每行着色,请执行以下操作:

So to color each line according to a variable, do something like this:

numlines = 20

for i in np.linspace(0,1, numlines):
    plt.plot(np.arange(numlines),np.tile([i],numlines), linewidth=4, color=plt.cm.RdYlBu(i))

这篇关于matplotlib中的线图颜色随线索引的数据索引而变化吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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