使用 matplotlib 创建超过 20 种独特的图例颜色 [英] creating over 20 unique legend colors using matplotlib

查看:44
本文介绍了使用 matplotlib 创建超过 20 种独特的图例颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 matplotlib 在一个图上绘制 20 条不同的线.我使用 for 循环绘制并用其键标记每一行,然后使用图例函数

I am plotting 20 different lines on a single plot using matplotlib. I use a for loop for plotting and label every line with its key and then use the legend function

for key in dict.keys():
    plot(x,dict[key], label = key)
graph.legend()

但是使用这种方式,图表在图例中重复了很多颜色.有什么方法可以确保使用 matplotlib 和 20 多行为每行分配唯一颜色?

But using this way, the graph repeats a lot of colors in the legend. Is there any way to ensure a unique color is assigned to each line using matplotlib and over 20 lines?

谢谢

推荐答案

您的问题的答案与另外两个 SO 问题有关.

The answer to your question is related to two other SO questions.

如何在 matplotlib 中为图形中的每条绘制线选择新颜色? 解释了如何定义循环选择下一个要绘制的颜色的默认颜色列表.这是通过 Axes.set_color_cycle 方法.

The answer to How to pick a new color for each plotted line within a figure in matplotlib? explains how to define the default list of colors that is cycled through to pick the next color to plot. This is done with the Axes.set_color_cycle method.

不过,您想获得正确的颜色列表,使用颜色图最容易做到这一点,如此问题的答案中所述:从 matplotlib 中的给定颜色图创建颜色生成器.有一个颜色映射从 0 到 1 的值并返回一个颜色.

You want to get the correct list of colors though, and this is most easily done using a color map, as is explained in the answer to this question: Create a color generator from given colormap in matplotlib. There a color map takes a value from 0 to 1 and returns a color.

因此,对于您的 20 行,您希望以 1/20 的步长从 0 循环到 1.具体来说,您希望从 0 到 19/20 循环,因为 1 映射回 0.

So for your 20 lines, you want to cycle from 0 to 1 in steps of 1/20. Specifically you want to cycle form 0 to 19/20, because 1 maps back to 0.

这是在这个例子中完成的:

This is done in this example:

import matplotlib.pyplot as plt
import numpy as np

NUM_COLORS = 20

cm = plt.get_cmap('gist_rainbow')
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_color_cycle([cm(1.*i/NUM_COLORS) for i in range(NUM_COLORS)])
for i in range(NUM_COLORS):
    ax.plot(np.arange(10)*(i+1))

fig.savefig('moreColors.png')
plt.show()

这是结果图:

替代的、更好的(有争议的)解决方案

还有一种替代方法是使用 ScalarMappable 对象将一系列值转换为颜色.这种方法的优点是您可以使用非线性Normalization 将行索引转换为实际颜色.以下代码产生完全相同的结果:

There is an alternative way that uses a ScalarMappable object to convert a range of values to colors. The advantage of this method is that you can use a non-linear Normalization to convert from line index to actual color. The following code produces the same exact result:

import matplotlib.pyplot as plt
import matplotlib.cm as mplcm
import matplotlib.colors as colors
import numpy as np

NUM_COLORS = 20

cm = plt.get_cmap('gist_rainbow')
cNorm  = colors.Normalize(vmin=0, vmax=NUM_COLORS-1)
scalarMap = mplcm.ScalarMappable(norm=cNorm, cmap=cm)
fig = plt.figure()
ax = fig.add_subplot(111)
# old way:
#ax.set_color_cycle([cm(1.*i/NUM_COLORS) for i in range(NUM_COLORS)])
# new way:
ax.set_color_cycle([scalarMap.to_rgba(i) for i in range(NUM_COLORS)])
for i in range(NUM_COLORS):
    ax.plot(np.arange(10)*(i+1))

fig.savefig('moreColors.png')
plt.show()

<小时>

弃用说明
在较新版本的 mplib (1.5+) 中,set_color_cycle 函数已被弃用,取而代之的是 ax.set_prop_cycle(color=[...]).


Deprecation Note
In more recent versions of mplib (1.5+), the set_color_cycle function has been deprecated in favour of ax.set_prop_cycle(color=[...]).

这篇关于使用 matplotlib 创建超过 20 种独特的图例颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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