Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change line width of specific line in line plot pandas/matplotlib

I am plotting a dataframe that looks like this.

Date    2011    2012    2013    2014    2015    2016    2017    2018    2019    2020
Date                                        
01 Jan  12.896  13.353  12.959  13.011  13.073  12.721  12.643  12.484  12.876  13.102
02 Jan  12.915  13.421  12.961  13.103  13.125  12.806  12.644  12.600  12.956  13.075
03 Jan  12.926  13.379  13.012  13.116  13.112  12.790  12.713  12.634  12.959  13.176
04 Jan  13.051  13.414  13.045  13.219  13.051  12.829  12.954  12.724  13.047  13.187
05 Jan  13.176  13.417  13.065  13.148  13.115  12.874  12.956  12.834  13.098  13.123

The code for plotting is here.

ice_data_dates.plot(figsize=(20,12), title='Arctic Sea Ice Extent', lw=3, fontsize=16, ax=ax, grid=True)

This plots a line plot for each of the years listed in the dataframe over each day in the year. However, I would like to make the line for 2020 much thicker than the others so it stands out more clearly. Is there a way to do that using this one line of code? Or do I need to manually plot all of the years such that I can control the thickness of each line separately? A current picture is attached, where the line thicknesses are all the same.

enter image description here

like image 264
Eli Turasky Avatar asked Jun 19 '20 12:06

Eli Turasky


1 Answers

You can iterate over the lines in the plot, which can be retrieved with ax.get_lines, and increase the width using set_linewidth if its label matches the value of interest:

fig, ax = plt.subplots()
df.plot(figsize=(20,12), title='Arctic Sea Ice Extent', 
        lw=3, fontsize=16, ax=ax, grid=True)

for line in ax.get_lines():
    if line.get_label() == '2020':
        line.set_linewidth(15)
plt.show()

enter image description here

like image 179
yatu Avatar answered Sep 30 '22 05:09

yatu