2011-05-17 8 views
5

Estoy comparando algunos resultados algorítmicos usando matplotlib.pyplot, sin embargo, es muy difícil entender lo que está pasando ya que varias líneas tienen el mismo color exacto. Hay alguna manera de evitar esto? No creo que pyplot solo tenga siete colores, ¿o sí?Cómo evitar la repetición de color de línea en matplotlib.pyplot?

+0

http://matplotlib.sourceforge.net/users/pyplot_tutorial.html – Ion

+0

Te sugiero que eches un vistazo a esta publicación: http://stackoverflow.com/questions/4805048/how-to-get-different-li nes-for-different-plots-in-a-one-figure –

Respuesta

5

Matplotlib tiene más de siete colores. Puede especificar su color de muchas maneras (consulte http://matplotlib.sourceforge.net/api/colors_api.html).

Por ejemplo, puede especificar el color utilizando una cadena HTML hexadecimal:

pyplot.plot(x, y, color='#112233') 
5

Lo mejor si sabe cuántas parcelas se va a trama es definir el mapa de colores antes:

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) 

Usando nipy_spectral

enter image description here

Uso de Set1 enter image description here

+1

¿Debería preocuparnos que 'set_color_cycle()' esté en desuso en 'versión 1.5'? 'plt' sugiere usar' set_prop_cycle() 'en su lugar (pero aún no lo he intentado). – dwanderson

2

También sugeriría el uso de Seaborn. Con esta biblioteca es muy fácil generar paletas de colores secuenciales o cualitativas con la cantidad de colores que necesita. También hay una herramienta para visualizar las paletas. Por ejemplo:

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") 

Estas son las paletas resultantes:

enter image description here

enter image description here

enter image description here

Cuestiones relacionadas