-
Notifications
You must be signed in to change notification settings - Fork 400
Description
Hi Yan,
in the Graph Gallery in some example in Python there are mistakes
for example in the "Show Number of Observations"
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import pandas as pd
Dataset:
a = pd.DataFrame({ 'group' : np.repeat('A',500), 'value': np.random.normal(10, 5, 500) })
b = pd.DataFrame({ 'group' : np.repeat('B',500), 'value': np.random.normal(13, 1.2, 500) })
c = pd.DataFrame({ 'group' : np.repeat('B',500), 'value': np.random.normal(18, 1.2, 500) })
d = pd.DataFrame({ 'group' : np.repeat('C',20), 'value': np.random.normal(25, 4, 20) })
e = pd.DataFrame({ 'group' : np.repeat('D',100), 'value': np.random.uniform(12, size=100) })
df = pd.concat([a,b,c,d,e])
Start with a basic boxplot
sns.boxplot(x="group", y="value", data=df) # the assignment ax is missing
Calculate number of obs per group & median to position labels
medians = df.groupby(['group'])['value'].median().values
nobs = df.groupby("group").size().values
nobs = [str(x) for x in nobs.tolist()]
nobs = ["n: " + i for i in nobs]
Add it to the plot
pos = range(len(nobs))
for tick,label in zip(pos,ax.get_xticklabels()):
plt.text(pos[tick], medians[tick] + 0.4, nobs[tick], horizontalalignment='center', size='medium', color='w', weight='semibold')
add title
plt.title("Boxplot with number of observation", loc="left")
show the graph
plt.show()
================
Best regards
--Marco