2016-10-17 70 views
0

绘图为每一行生成不同的颜色,但我也需要为该图生成不同的line_styles。搜索了一些信息后,我找到了itertools模块。但是我无法生成错误图:没有Line2D属性“shape_list”。我无法迭代line_styles(Matplotlib)

import itertools 
from glob import glob 
import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib as mpl 

shape_list = ["square", "triangle", "circle", "pentagon", "star", "octagon"] 

# loop over all files in the current directory ending with .txt 
for fname in glob("*.txt"): 
    # read file, skip header (1 line) and unpack into 3 variables 
    WL, ABS, T = np.genfromtxt(fname, skip_header=1, unpack=True) 
    g = itertools.cycle(shape_list) 
    plt.plot(WL, T, label=fname[0:3],shape_list = g.__next__()) 

plt.xlabel('Wavelength (nm)') 
plt.xlim(200,1000) 
plt.ylim(0,100) 
plt.ylabel('Transmittance (%)') 
mpl.rcParams.update({'font.size': 12}) 
plt.legend(loc=4,prop={'size':10}) 
plt.grid(True) 
#plt.legend(loc='lower center') 
plt.savefig('Transmittance', dpi=600) 

回答

1

你可以plot使用标记定义in the documentation

要更改标记样式,请使用marker= a rgument号召,plot()

如:

plt.plot(WL, T, label=fname[0:3], marker=g.__next__()) 

编辑

我把这里一个完整的答案,关闭这个问题

# list of symbols 
shape_list = ["s", "^", "o", "*", "p", "h"] 
g = itertools.cycle(shape_list) # so we can cycle through the list of symbols 

# some fake data 
x = np.linspace(200,1000,1000) 
y = [x+100*b for b in range(6)] 

# loop over files 
for i in range(6): # I'm simultating a loop here since I don't have any files 
    # read file 
    # do your plot 
    # we use `g.next()` to get the next marker in the cycle 
    # and `markevery` to only plot a few symbols so they dont overlap 
    # adjust the value for your specific data 
    plt.plot(x,y[i], marker=g.next(), markevery=100, label=i) 

plt.xlabel('Wavelength (nm)') 
plt.ylabel('Transmittance (%)') 
plt.legend(loc=4,prop={'size':10}) 
plt.grid(True) 
#plt.legend(loc='lower center') 
#plt.savefig('Transmittance', dpi=600) 

enter image description here

+0

是的。它可以工作,但线条无法区分。看起来他们有背景和频率高。很难区分圆形和方形。 – esilik

+1

如果您的标记太多以至于无法区分,则可以使用'markevery ='仅在每个x点显示一个标记。请参阅http://matplotlib.org/examples/pylab_examples/markevery_demo.html –

+0

非常有用的评论。谢谢! – esilik

1

我认为g = itertools.cycle(shape_list)应该去外循环

另见here的有效标记 你可能想要的是 plt.plot(WL, T, label=fname[0:3], marker = g.__next__())

+0

是。我刚刚尝试过,但我用linestyle代替标记。我认为他们之间有区别。 – esilik

+0

[http://nullege.com/codes/search/matplotlib.lines.Line2D.set_linestyle](这不是我要找的)。我正在寻找[http://matplotlib.org/1.3.1/examples/pylab_examples/line_styles.html](this)。 – esilik

+0

'linestyle'更通用。我认为你应该使用标记。如果你想要比标准颜色更多的颜色,你可以用'颜色'做同样的操作。 – kameranis