使用循环变量在matplotlib中指定颜色

时间:2012-07-08 15:41:01

标签: python matplotlib

我有很多数据文件,我想在同一个地块上绘制所有数据,但颜色不同。我使用以下代码

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

可能我可以使用整数并使用整数来指定绘图的颜色。有人可以帮我解释语法吗?

1 个答案:

答案 0 :(得分:1)

如果文件/图的数量很小,你可以制作一个颜色数组,其长度与上面的数组相同:如:


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