之后设置绘图线型

Setting plot linestyles afterwards

我正在循环绘制图表:

cities = grouped_price.index.levels[0]  # list of cities
dates  = grouped_price.index.levels[1]  # list of dates, which
                                        # are 1st day of each month
linestyles = ['-', '-.', '--', '-.-', ':']

for city in cities[0:1]:
    for month in dates:  # loop over dates, which are grouped by month
        legend = legend + [month.strftime("%B")] # month in text
        ax = grouped_price.loc[city, month]['Amount'].plot()
plt.show()

之后如何设置 linestyles?如果我写

ax = grouped_price.loc[city, week]['Amount'].plot(style = linestyles)

在循环内部,它只对所有行使用第一个 linestyle

colors 和行 thickness 相同的问题。我找到了一个设置厚度的迭代解决方案(在每条线上循环),但是有更简单的方法吗? 谢谢

如果你有相同的月数和线型,你可以这样做:

linestyles = ['-', '-.', '--', '-.-', ':']
for city in cities[0:1]:
    for month,ls in zip(dates, linestyles):  # loop over dates, which are grouped by month and linestyles
        legend = legend + [month.strftime("%B")] # month in text
        ax = grouped_price.loc[city, month]['Amount'].plot(style = ls)
plt.show()