使用matplotlib创建20多种独特的图例颜色 [英] creating over 20 unique legend colors using matplotlib
问题描述
我正在使用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.
不过,您希望获得正确的颜色列表,这很容易使用颜色图完成,如对此问题的答案所述:
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()
这是结果图:
替代的,更好的(值得商bat的)解决方案
还有另一种使用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屋!