I have a side-by-side boxplot like this
import pandas as pd
import numpy as np
group = np.array([ np.random.binomial(2,0.4) for _ in range(100)])
data = [ np.random.uniform(0,5) for _ in range(len(group))]
df = pd.DataFrame({'data': data, 'group': group} )
df.boxplot(by='group', vert=False)
I am trying to connect the 25, 50 and 75 quantiles of these boxplots together, my desired output is something like this:
I wonder whether there is a way to do this with pandas/matplotlib
CodePudding user response:
You can use:
ax = df.boxplot(by='group', vert=False)
quantiles = df.groupby('group')['data'].quantile([0.25, 0.5, 0.75]).unstack()
for col in quantiles:
ax.plot(quantiles[col], range(1, len(quantiles) 1), label=col)
plt.legend()
If you had a vertical boxplot, this would be a bit easier:
ax = df.boxplot(by='group', vert=True)
quantiles = df.groupby('group')['data'].quantile([0.25, 0.5, 0.75]).unstack()
quantiles.set_axis(range(1, len(quantiles) 1)).plot(ax=ax)