Joss
Joss

Reputation: 197

Pandas matplotlib boxplot with mean trendline

I'd like to add a trendline to my boxplot showing the mean values. Anyone figured it out using pandas?

My code goes like that:

fig, ax = plt.subplots(figsize=(10,5))
ax1 = df.boxplot(column='val', by='DATE',ax=ax)

And I get a nice boxplot as a result.

enter image description here

Now how can I add a line to connect the mean values?

Thanks.

Upvotes: 0

Views: 2097

Answers (1)

smj
smj

Reputation: 1284

Do you mean you want to plot the means? If so you can pass showmeans = True to the boxplot, and it will use a marker to show the (arithmetic) mean. My personal opinion is that this will look better than a line superimposed on the boxplot (which is also possible to do):

import pandas as pd
import numpy as np

data = pd.DataFrame(
    {
        'date_id': np.tile(pd.date_range('2018-10-01', '2018-10-5').astype(str), 10),
        'value': np.random.randn(50)
    }
)

data.boxplot(column = 'value', by = 'date_id', showmeans = True)

Gives:

enter image description here

Upvotes: 2

Related Questions