0

我正在循环绘制图形:

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()

之后如何设置线条样式?如果我写

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

在循环内部,它只对所有行使用第一个线条样式。

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

4

1 回答 1

1

如果您有相同的月数和线条样式,您可以执行以下操作:

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()
于 2018-07-05T09:33:17.960 回答