2

I have the following code which creates a box plot of APOE4 gene copies and memory scores (hence the variable names)

clean_merged.boxplot('composite scores', by='APOE4', widths = 0.8, showmeans = True, meanline = True)
plt.title('Mean Composite Memory Score by APOE4 Copies')
plt.xlabel('Number of APOE4 Copies')
plt.ylabel('Composite Memory Score')
plt.rcParams['figure.figsize'] = (10,10)

which generates this output:

enter image description here I would like to add a legend for the mean (green dashed) and median lines (solid fill) and am struggling to do so based on previously asked questions as how i have created my graph is quite different to the methods usually advised.

I'm new to python so any help is appreciated :) thanks !!!

Finn Hull
  • 41
  • 2

1 Answers1

4

You can add "empty" lines to the plot, assigning them the appropriate styles, colors, and labels. These will be picked up by plt.legend(). Since I don't have your data, I'm using seaborn's Titanic dataset as an example:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()

titanic = sns.load_dataset('titanic')
titanic.boxplot('age', by='sex', widths=0.8, showmeans=True, meanline=True)

plt.plot([], [], '-', linewidth=1, color='Crimson', label='mean')
plt.plot([], [], '-', linewidth=1, color='gray', label='median')

plt.legend()

arbitrary legend

Arne
  • 9,990
  • 2
  • 18
  • 28