我无法迭代line_styles(Matplotlib)

时间:2016-10-17 08:38:25

标签: python matplotlib

Plot为每一行生成不同的颜色,但我还需要为图形生成不同的line_styles。在搜索了一些信息之后,我找到了itertools模块。然而,我无法生成错误的情节:没有Line2D属性“shape_list”。

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)

2 个答案:

答案 0 :(得分:1)

我认为g = itertools.cycle(shape_list)应该超出循环次数

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

答案 1 :(得分:1)

您可以使用plot标记的标记in the documentation

要更改标记样式,请使用marker=参数调用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