Is there a better way of adding a single label to a legend for a set of boxplots?
Below is a simple worked example that gives the desired result. This is done my creating an invisible line (alpha=0
) with the desired label, then changing the alpha via the legendHandles
. however can a single label for all the boxplots just be passed to sns.boxplot()
?
import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Get the tips dataset and select a subset as an example
tips = sns.load_dataset("tips")
variable_to_bin_by = 'tip'
binned_variable = 'total_bill'
df = tips[ [binned_variable, variable_to_bin_by] ]
# Group the data by a list of bins
bins = np.array([0, 1, 2, 3, 4])
gdf = df.groupby( pd.cut(df[variable_to_bin_by].values, bins ) )
data = [ i[1][binned_variable].values for i in gdf]
df = pd.DataFrame( data, index = bins[:-1])
# Plot the data (using boxplots to show spread of real values)
fig, ax = plt.subplots()
ax = sns.boxplot( data=df.T, ax=ax, color='k')
# Create hidden line with the extra label (to give label to boxplots)
x = np.range(10)
plt.plot(x, x, label='REAL DATA', color='k', alpha=0)
# Now plot some "model fit" lines
models = {'model1': bins+10, 'model2': bins+10*1.5, 'model3': bins*10}
for key in sorted( models.keys() ):
plt.plot( bins, models[key], label=key )
# Add a legend
leg = plt.legend()
# Update line visibility (alpha)
for legobj in leg.legendHandles:
legobj.set_alpha( 1 )
# Show the plot
plt.show()
Although this gives the desired result (as below), my question is whether there a better way?