2

I have a dictionary that stores the following values in order for each user ID:

  • Min
  • Lower Quartile
  • Median
  • Upper Quartile
  • Max

When plotting the boxplot, I would like the min and max from the dictionary to be used in the legs of the boxplot chart. At the moment, it plots these as outliers and I would like them to be plotted as part of the box chart's legs.

My code:

for ID in Data:
    #Min
    Data1[ID].append(10)
    #LQ
    Data1[ID].append(20)
    #Median
    Data1[ID].append(30)
    #UQ
    Data1[ID].append(40)
    #Max
    Data1[ID].append(50)
#Plot
fig, ax = plt.subplots()
ax.boxplot(Data1.values())
Tom Pitts
  • 305
  • 1
  • 4
  • 6

1 Answers1

5

The legs are called whiskers and you can control them by setting the parameter whis in the boxplot call. In your case you probably want to set it to 'range'

ax.boxplot(Data1.values(), whis='range')

as stated in the documentation:

whis : float, sequence, or string (default = 1.5) As a float, determines the reach of the whiskers to the beyond the first and third quartiles. In other words, where IQR is the interquartile range (Q3-Q1), the upper whisker will extend to last datum less than Q3 + whis* IQR). Similarly, the lower whisker will extend to the first datum greater than Q1 - whis* IQR. Beyond the whiskers, data are considered outliers and are plotted as individual points. Set this to an unreasonably high value to force the whiskers to show the min and max values. Alternatively, set this to an ascending sequence of percentile (e.g., [5, 95]) to set the whiskers at specific percentiles of the data. Finally, whis can be the string 'range' to force the whiskers to the min and max of the data.

Note that you don't plot boxplots of your actual data, but a boxplot of the summary statistics you provide. This is usually different. If you want to use your summary statistics to plot a boxplot of the original data have a look at this example and the matplotlib method bxp.

CodeZero
  • 1,649
  • 11
  • 18