简体   繁体   English

Matplotlib:为行指定颜色

[英]Matplotlib: Assign Colors to Lines

I am trying to plot a variable number of lines in matplotlib where the X, Y data and colors are stored in numpy arrays, as shown below. 我试图在matplotlib中绘制可变数量的行,其中X,Y数据和颜色存储在numpy数组中,如下所示。 Is there a way to pass an array of colors into the plot function, so I don't have to take an extra step to assign a color to each line individually? 有没有办法将一组颜色传递给绘图函数,所以我不需要额外的步骤为每一行分别分配颜色? Should I be translating the RGB color arrays to another color format for this to work, such as HSV or other? 我是否应该将RGB颜色数组转换为另一种颜色格式才能使其工作,例如HSV或其他?

import numpy as np
X = np.arange(1990, 1994)
Y = [[  1.50615936e+08   5.88252480e+07   2.60363587e+08]
     [  1.53193798e+08   5.91663430e+07   2.63123995e+08]
     [  1.55704596e+08   5.94899260e+07   2.65840188e+08]
     [  1.58175186e+08   5.97843680e+07   2.68559452e+08]]
colors = [(0.99609375, 0.3984375, 0.3984375) (0.796875, 0.0, 0.99609375)
          (0.59765625, 0.99609375, 0.0)]
#current way
ax.plot(X, Y)
[ax.lines[i].set_color(color) for i, color in enumerate(colors)]
#way I feel it can be done, but doesn't work currently
ax.plot(X, Y, color=colors)
plt.show()

Any help is greatly appreciated. 任何帮助是极大的赞赏。

I think you want to use the Axes method set_color_cycle . 我想你想使用Axes方法set_color_cycle As you can imagine, it sets the list of colors that are cycled through when colors are assigned by default, ie when no color keyword is provided to the plot call. 可以想象,它设置了默认情况下分配颜色时循环使用的颜色列表,即没有为plot调用提供颜色关键字时。 Here's an extended version of your example: 这是您的示例的扩展版本:

import matplotlib.pyplot as plt
import numpy as np

X = np.arange(1990, 1994)
Y = [[  1.50615936e+08,   5.88252480e+07,   2.60363587e+08],
     [  1.53193798e+08,   5.91663430e+07,   2.63123995e+08],
     [  1.55704596e+08,   5.94899260e+07,   2.65840188e+08],
     [  1.58175186e+08,   5.97843680e+07,   2.68559452e+08]]
colors = [(0.99609375, 0.3984375, 0.3984375), 
          (0.796875, 0.0, 0.99609375),
          (0.59765625, 0.99609375, 0.0)]

fig = plt.figure()
ax1 = fig.add_subplot(211)
ax1.set_title('old way')
ax1.plot(X, Y)
[ax1.lines[i].set_color(color) for i, color in enumerate(colors)]

ax2 = fig.add_subplot(212)
ax2.set_title('new way')
ax2.set_color_cycle(colors)
ax2.plot(X, Y)

fig.savefig('manycolors.py')
plt.show()

This results in two subplots with the identically colored lines: 这导致两个子图具有相同颜色的线:

在此输入图像描述

There is still a newer "new way" than that suggested by @Yann, since Matplotlib version 1.5. 自Matplotlib版本1.5以来,仍有一种比@Yann所建议的更新的 “新方式”。 Instead of set_color_cycle use (depricated) you should use set_prop_cycle . 您应该使用set_prop_cycle而不是set_color_cycle使用(depricated)。 Here you have his example redone. 在这里你重复他的例子。 I also recommend you to use Seaborn which has lots of pre-difined palettes where you can chose the number of colours. 我还建议你使用Seaborn ,它有很多预先调制的调色板,你可以选择颜色的数量。 The palette colours are based on Colorbrewer , a tool to select good color maps. 调色板颜色基于Colorbrewer ,这是一种选择优质色彩图的工具。 So this is my version of @Yann code: 所以这是我的@Yann代码版本:

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

X = np.arange(1990, 1994)
Y = [[  1.50615936e+08,   5.88252480e+07,   2.60363587e+08],
     [  1.53193798e+08,   5.91663430e+07,   2.63123995e+08],
     [  1.55704596e+08,   5.94899260e+07,   2.65840188e+08],
     [  1.58175186e+08,   5.97843680e+07,   2.68559452e+08]]

colors = sns.color_palette("hls", len(Y[0]))

fig = plt.figure()
ax1 = fig.add_subplot(211)
ax1.set_title('old way')
ax1.plot(X, Y)
[ax1.lines[i].set_color(color) for i, color in enumerate(colors)]

ax2 = fig.add_subplot(212)
ax2.set_title('new way')
ax2.set_prop_cycle('color', colors)
ax2.plot(X, Y)

plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM