2016-10-17 69 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