A box plot is a graph that shows the distribution of data, including quantities: minimum value (min), maximum value (max), median, first quartile (Q1), first quartile (Q3).
Components of a box in the box plot
Draw a box plot with Python using matplotlib.pyplot.boxplot()
:
import matplotlib.pyplot as plt
import numpy as np
# Generate some sample data
data = [np.random.normal(0, 1, 100),
np.random.normal(2, 1.5, 100),
np.random.normal(-1, 0.5, 100)]
for i in range(len(data)):
print(f"Data {i}:\\n {data[i]}")
# Create the box plot
plt.boxplot(data)
# Add labels and title
plt.title('Box Plot of Multiple Datasets')
plt.xlabel('Dataset')
plt.ylabel('Value')
# Show the plot
plt.show()
Output:
Data 0:
[ 1.20996633 0.99075303 -1.38660836 2.84450598 0.15427753 -1.08717568
-1.40939774 0.60165096 0.46467452 0.20513199 -0.72728194 1.12814642
1.06940616 -0.4652316 0.56474326 -0.51340852 0.48401981 -0.19086204
-0.35745017 0.09546895 0.51185797 -1.25341807 -0.04518539 -0.60116898
-1.01196689 -0.19345498 1.02771627 -0.67782115 -0.05869647 0.99576391
-0.10600939 -0.26592461 -2.01186714 -0.37490462 1.79536533 -0.20543103
0.42596091 -0.13945345 0.12045901 1.0086201 -0.89869643 -0.04254307
1.10688081 0.12188057 -0.61153163 -0.29844204 -0.90910525 -2.0251287
-0.01085386 1.0421397 -0.06735044 -0.53860875 0.19807234 2.07518453
-1.56031354 1.63430707 -0.62560665 0.37571056 -0.59413804 1.32204044
-0.53097343 -0.17689534 0.3712165 1.87867291 0.22666835 -1.69716764
0.52160377 0.07070101 -3.25013065 0.83366539 0.22277452 -0.95948214
0.43827986 0.19603409 -0.30316865 -0.40046073 -0.57231863 -1.78839764
1.44505002 -0.62746014 0.29884768 -0.39095519 0.42442474 1.21403
0.01693482 -2.17784896 -1.03587541 -1.08533381 -0.40137081 0.84872951
0.58777254 0.9749014 -0.90936936 0.3856791 0.31055405 0.48182356
-0.20366851 0.50419659 -0.7548318 0.32441853]
Data 1:
[ 0.60844259 0.04957763 3.18106563 1.05496248 4.77730842 2.08147437
-0.01879531 1.84612061 3.27810464 -0.68999636 0.06175414 2.20332934
1.09658498 1.28077269 4.90140283 4.64448289 1.76289347 1.24939204
1.53338829 1.1739012 1.79460243 2.23415525 2.05524894 1.56688667
0.86163328 2.23870322 2.74888 1.75176109 1.87546774 1.26897331
1.21190279 3.37577133 2.19679035 0.65482109 0.32143933 1.50731754
2.00913517 -0.67860549 4.90213561 1.12497686 -1.31902464 3.32082849
2.94167687 4.58923936 2.15232821 2.03019659 1.01271384 1.11287118
-0.7653833 5.78478923 1.84350852 6.10299006 1.30983556 2.46915847
-0.15297881 0.94684455 1.58855406 1.28442367 1.70351061 0.85810885
1.11787311 0.70002329 4.23013428 3.52302342 1.13697968 1.65536371
2.56718127 3.31565893 4.49482357 2.95453306 2.80497863 2.919432
2.35997156 0.89294614 2.94263061 -0.44622099 2.82864349 1.08539785
1.46542794 1.34453623 0.53249784 2.10477639 -0.90556913 2.0108126
-0.61452236 2.60123148 2.60899489 4.46769431 3.27751543 0.7545254
0.73474781 2.37496521 1.05962625 3.09657163 3.54493569 0.87232155
-1.08853975 2.27172267 5.32110188 0.76030943]
Data 2:
[-0.55260253 -1.31098337 -1.34067382 -0.97945624 -1.2892417 -0.85457477
-0.58802696 -0.75450278 -0.3360143 -1.48506047 -1.92266702 -0.34726217
-1.19702642 -1.41709682 0.13402441 -2.2273311 -1.17811386 -0.69592896
-0.98699504 -2.42095407 -2.057995 -0.73635016 -0.66395936 -1.44008601
-0.22874745 -0.94966835 -1.37877416 -1.4258617 -1.25491338 -1.30996455
-1.04266459 -1.20721064 -1.06050846 -1.15971465 -0.75011847 -0.73100133
-1.57625103 -0.65332886 -0.35491526 -1.85941085 -0.48396146 -0.33136224
-0.71882633 -1.98338336 -1.30978503 -0.06097007 -1.75776383 -0.27094597
-1.08025959 -1.33060317 -1.58416828 -0.9329439 -1.12067912 -0.95018622
-1.42850172 -0.5241719 -1.07733374 -0.21626499 -0.80704972 -0.60744062
-0.84656009 -0.35492847 -0.61962293 -0.29908358 -1.6836995 0.11123658
-0.56550582 -1.10356802 -1.25730635 -1.0723217 -1.33946498 -1.05252666
-0.42154784 -1.11597955 -0.55698416 -0.94153347 -0.73440302 -1.43934445
-1.06732966 -1.19156556 -0.62446854 -0.74302872 -1.82752492 -0.9900566
-1.02773029 -0.93258778 -0.95115815 -0.60256354 -0.9187448 -1.45050391
-0.48355988 -0.51502817 -0.50542874 -1.19860653 -0.48469302 -1.75180865
-1.36367679 -2.25039712 -0.67508425 -0.88007868]