median = 3637
std = 1274.997414
perc_25 = 2627.0
perc_75 = 4238.0
I have 4 values derived from data. How can I make a Boxplot out of this? I expect a line symbolizing median, a box delimited by 25-percentile and 75-percentile and one point on each side for median+std and median-std.
Usually I need a list of values or a dataframe, but I already computed the statistics, now I just want to display them.
Internally, boxplot
computes bxpstats
with matplotlib.cbook.boxplot_stats
(source), then it passes the result to Axes.bxp
(source).
bxpstats = cbook.boxplot_stats(x, whis=whis, bootstrap=bootstrap,
labels=labels, autorange=autorange)
...
artists = self.bxp(bxpstats, positions=positions, widths=widths,
vert=vert, patch_artist=patch_artist,
shownotches=notch, showmeans=showmeans,
showcaps=showcaps, showbox=showbox,
boxprops=boxprops, flierprops=flierprops,
medianprops=medianprops, meanprops=meanprops,
meanline=meanline, showfliers=showfliers,
capprops=capprops, whiskerprops=whiskerprops,
manage_ticks=manage_ticks, zorder=zorder,
capwidths=capwidths)
You can shortcut the first steps by designing a dictionary in the correct format:
import matplotlib.pyplot as plt
median = 3637
std = 1274.997414
perc_25 = 2627.0
perc_75 = 4238.0
bxpstats = [{'whishi': median+std,
'whislo': median-std,
'fliers': [],
'q1': perc_25,
'med': median,
'q3': perc_75}]
ax = plt.subplot()
ax.bxp(bxpstats)
Output:
If you want several boxes add more dictionaries in the list:
bxpstats = [{'whishi': 5, 'whislo': 1, 'fliers': [6], 'q1': 2, 'med': 3, 'q3': 4},
{'whishi': 5.5, 'whislo': 3, 'fliers': [2, 2.5, 5.7], 'q1': 4, 'med': 4.5, 'q3': 5}
]
ax = plt.subplot()
ax.bxp(bxpstats)
To give you the full list of parameters:
import matplotlib
matplotlib.cbook.boxplot_stats([1, 2, 3, 100])
[{'mean': 26.5, # mean (shown if showmeans=True)
'iqr': 25.5, # q3-q1
'cilo': -17.517500000000002, # lower confidence interval (shown if shownotches=True)
'cihi': 22.517500000000002, # upper confidence interval (shown if shownotches=True)
'whishi': 27.25, # upper whisker
'whislo': 1, # lower whisker
'fliers': array([100]), # outliers
'q1': 1.75, # q1 (bottom of box)
'med': 2.5, # median (orange line)
'q3': 27.25}] # q3 (top of box)