I would like to show some values in a Seaborn box plot such as Total# observations, Mean, Mix/Max value for each box plot series. Is there a way to show these in the plot?
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_theme(style="ticks")
# Initialize the figure with a logarithmic x axis
f, ax = plt.subplots(figsize=(7, 6))
ax.set_xscale("log")
# Load the example planets dataset
planets = sns.load_dataset("planets")
# Plot the orbital period with horizontal boxes
sns.boxplot(
planets, x="distance", y="method", hue="method",
whis=[0, 100], width=.6, palette="vlag"
)
# Add in points to show each observation
sns.stripplot(planets, x="distance", y="method", size=4, color=".3")
# Tweak the visual presentation
ax.xaxis.grid(True)
ax.set(ylabel="")
sns.despine(trim=True, left=True)
Example output:
1
Just use matplotlib text function with the “data coordinates” for x and y:
mytext = 'Total: XX n Mean: YY n Min Value: AA n Max Value: BB'
ax.text(x=15000, y='Radial Velocity',
s=mytext, style='italic',
color='white', bbox = {'facecolor': 'black'},
fontsize=8, verticalalignment='center')
plt.show()
I made it a function:
def add_bplot_annotations(df, x, y, ax):
stats = df.groupby(y)[x].describe()
ticklabels = ax.get_yticklabels()
transform = ax.get_yaxis_transform()
for t in ticklabels:
lbl = t.get_text()
total = int(stats.loc[lbl, "count"])
mean = stats.loc[lbl, "mean"]
min_value = stats.loc[lbl, "min"]
max_value = stats.loc[lbl, "max"]
stats_str = f"Total: {total}nMean: {mean:.2f}nMin Value: {min_value:.2f}nMax Value: {max_value:.2f}"
ax.text(
x=1,
y=lbl,
s=stats_str,
style="italic",
color="white",
bbox={"facecolor": "black"},
fontsize=8,
verticalalignment="center",
transform=transform,
)
Result:
Full code:
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_theme(style="ticks")
# Initialize the figure with a logarithmic x axis
f, ax = plt.subplots(figsize=(8, 7))
ax.set_xscale("log")
# Load the example planets dataset
planets = sns.load_dataset("planets")
x = "distance"
y = "method"
# Plot the orbital period with horizontal boxes
sns.boxplot(planets, x=x, y=y, hue="method", whis=[0, 100], width=0.6, palette="vlag")
# Add in points to show each observation
sns.stripplot(planets, x=x, y=y, size=4, color=".3")
# Tweak the visual presentation
ax.xaxis.grid(True)
ax.set(ylabel="")
sns.despine(trim=True, left=True)
# Add annotations
add_bplot_annotations(planets, x, y, ax)
ax.figure.tight_layout()
plt.show()