pythonmatplotlibpandas-groupbylinestyle

Setting plot linestyles afterwards


I am plotting graphs in a loop:

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

How can I set linestyles afterwards? If I write

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

inside the loops, it just uses the first linestyle for all lines.

Same question with colors and line thickness. I found an iterative solution for setting thicknesses (loop over each line), but is there a simpler way? Thanks.


Solution

  • provided you have the same number of months and linestyles, you can do:

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