2011-05-17 382 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