加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-20 13:28:53 所属栏目:Python 来源:网络整理
导读:我有很多数据文件,我想在同一个图上绘制所有数据,但颜色不同.我使用以下代码 from pylab import loadtxt,average,std,argsortfrom os import listdirfrom fnmatch import fnmatchimport matplotlib.pyplot as plta=[]for file in listdir('.'): if fnmatch(f
我有很多数据文件,我想在同一个图上绘制所有数据,但颜色不同.我使用以下代码

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],yerr=m_abs_err[T_sort],fmt='ro')
  plt.semilogx(T[T_sort],m_phy[T_sort],'r^-')
  plt.errorbar(T[T_sort],yerr=m_phy_err[T_sort],fmt='r^')


plt.show()

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

解决方法

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读