2011-05-17 385 views
5

我正在比较一些使用matplotlib.pyplot的算法结果,但是很难理解发生了什么,因为几行代码具有相同的确切颜色。有没有办法避免这种情况?我不认为pyplot只有七种颜色,对吗?如何避免matplotlib.pyplot中的线条颜色重复?

+0

http://matplotlib.sourceforge.net/users/pyplot_tutorial.html – Ion 2011-05-17 07:42:30

+0

我建议你看看这篇文章:http://stackoverflow.com/questions/4805048/how-to-get-different-li nes-for-different-plots-in-one-figure – 2013-12-26 11:24:04

回答

5

的最好的事情,如果你知道是多少情节你要打印之前定义颜色表:

import matplotlib.pyplot as plt 
import numpy as np 

fig1 = plt.figure() 
ax1 = fig1.add_subplot(111) 
number_of_plots=10 
colormap = plt.cm.nipy_spectral #I suggest to use nipy_spectral, Set1,Paired 
ax1.set_color_cycle([colormap(i) for i in np.linspace(0, 1,number_of_plots)]) 
for i in range(1,number_of_plots+1): 
    ax1.plot(np.array([1,5])*i,label=i) 

ax1.legend(loc=2) 

使用nipy_spectral

enter image description here

使用Set1 enter image description here

+1

我们应该关注'set_color_cycle()'在版本1.5中被弃用吗? 'plt'建议使用'set_prop_cycle()'来代替(但我还没有尝试过)。 – dwanderson 2016-10-04 21:35:20

2

我也建议使用Seaborn。有了这个库,使用您需要的颜色数量可以很容易地生成顺序或定性调色板。还有一个工具来可视化调色板。例如:

import seaborn as sns 

colors = sns.color_palette("hls", 4) 
sns.palplot(colors) 
plt.savefig("pal1.png") 
colors = sns.color_palette("hls", 8) 
sns.palplot(colors) 
plt.savefig("pal2.png") 
colors = sns.color_palette("Set2", 8) 
sns.palplot(colors) 
plt.savefig("pal3.png") 

这些是所得到的调色板:

enter image description here

enter image description here

enter image description here