之后设置绘图线型

时间:2018-07-05 09:00:44

标签: python matplotlib pandas-groupby linestyle

我正在循环绘制图形:

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

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

1 个答案:

答案 0 :(得分: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()