Matplotlib通过Colormap用颜色绘制线 [英] Matplotlib Plot Lines with Colors Through Colormap

查看:344
本文介绍了Matplotlib通过Colormap用颜色绘制线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在一个绘图上绘制多条线,我希望它们遍历一个颜色图的光谱,而不仅仅是相同的6或7种颜色.代码类似于此:

I am plotting multiple lines on a single plot and I want them to run through the spectrum of a colormap, not just the same 6 or 7 colors. The code is akin to this:

for i in range(20):
     for k in range(100):
          y[k] = i*x[i]
     plt.plot(x,y)
plt.show()

我都使用colormap"jet"和从seaborn导入的另一个,得到了相同顺序重复的相同7种颜色.我希望能够绘制多达60条不同颜色的线.

Both with colormap "jet" and another that I imported from seaborn, I get the same 7 colors repeated in the same order. I would like to be able to plot up to ~60 different lines, all with different colors.

推荐答案

Matplotlib色图接受参数(0..1,标量或数组),该参数用于从色图获取颜色.例如:

The Matplotlib colormaps accept an argument (0..1, scalar or array) which you use to get colors from a colormap. For example:

col = pl.cm.jet([0.25,0.75])    

为您提供具有(两种)RGBA颜色的阵列:

Gives you an array with (two) RGBA colors:

array([[0.,0.50392157,1.,1.], [1.,0.58169935,0.,1.]]

array([[ 0. , 0.50392157, 1. , 1. ], [ 1. , 0.58169935, 0. , 1. ]])

您可以使用它来创建不同颜色的N:

You can use that to create N different colors:

import numpy as np
import matplotlib.pylab as pl

x = np.linspace(0, 2*np.pi, 64)
y = np.cos(x) 

pl.figure()
pl.plot(x,y)

n = 20
colors = pl.cm.jet(np.linspace(0,1,n))

for i in range(n):
    pl.plot(x, i*y, color=colors[i])

这篇关于Matplotlib通过Colormap用颜色绘制线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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