12

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

0

3 Answers 3

21

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

Sign up to request clarification or add additional context in comments.

1 Comment

can you increase the linewidth in the legend as well?
6

You can do it in two lines like this:

ice_data_dates.loc[:, ice_data_dates.columns != "2020"].plot(figsize=(20, 12), title='Arctic Sea Ice Extent', lw=3, fontsize=16, ax=ax, grid=True)
ice_data_dates["2020"].plot(figsize=(20, 12), title='Arctic Sea Ice Extent', lw=15, fontsize=16, ax=ax, grid=True, label="2020")

This will first plot the entire DataFrame except for the column for 2020 and then only plot 2020. The output looks like this: wider_line

This uses a different approach as the selected answer but it gives the same result, except that the legend entry of 2020 has the same linewidth as the line in the figure which can (not) be desirable.

2 Comments

But 2020 is missing from the legend.
@SampoSmolander updated the code to fix this.
0

We can add some code to the solution from the other answer, to change the line width in the legend as well:

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

for line, legend_line  in zip(ax.get_lines(), ax.legend().get_lines()):
    if line.get_label() == '2020':
        line.set_linewidth(15)
    if legend_line.get_label() == '2020':
        legend_line.set_linewidth(15)

plt.show()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.