Using a loop variable to specify color in matplotlib

I have many data files, and I want to build them all on the same plot, but with different colors. I am using the following code

from pylab import loadtxt, average, std, argsort from os import listdir from fnmatch import fnmatch import matplotlib.pyplot as plt a=[] for file in listdir('.'): if fnmatch(file,'A10data*'): a+=[str(file)] for file in a: T,m_abs, m_abs_err,m_phy,m_phy_err = loadtxt(file,unpack=True) T_sort = argsort(T) plt.xlim(0.00009,10.1) plt.ylim(-1,350) plt.semilogx(T[T_sort],m_abs[T_sort],'ro-') plt.errorbar(T[T_sort],m_abs[T_sort],yerr=m_abs_err[T_sort],fmt='ro') plt.semilogx(T[T_sort],m_phy[T_sort],'r^-') plt.errorbar(T[T_sort],m_phy[T_sort],yerr=m_phy_err[T_sort],fmt='r^') plt.show() 

Perhaps I can use an integer and use an integer to indicate the color of the graph. Can someone help me with the syntax?

+4
source share
1 answer

If the number of files / graphs is small, you can make an array of colors with the same length as the array called above: something like:

 colors = ["red", "blue" , "green", "orange", "purple"] ncolor = 0 for file in a: plt.semilogx(T[T_sort], m_abs[T_sort], 'o-', color=colors[ncolor]) ncolor+=1 
+1
source

All Articles