5

I have many files of data and I want to plot all of them 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()

Probably I can use an integer and use the integer to specify the color of the plot. Can someone help me with the syntax?

1
  • Side note: you should check out the glob standard module. Commented Jul 8, 2012 at 15:51

1 Answer 1

2

If the number of files/plots is small, you could make an array of colors that is the same length as array called a 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

Sign up to request clarification or add additional context in comments.

2 Comments

Slightly more pythonic you can use for file,c in zip(a,colors): and plt.semilogx(...,color=c)
Even more robust and pythonic: 'zip(a, itertools.cycle(colors))', which automatically cycles through the colors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.