Matplotlib boxplot show only max and minimum flyers

I make standard Matplotlib boxes using the plt.boxplot () command. My line of code that creates boxplot:

bp = plt.boxplot(data, whis=[5, 95], showfliers=True) 

Since my data is very widespread, I get many flyers out of the mustache range. In order to get a cleaner graph of publication quality, I would like to show only single fliers in max. and in minutes data values, not all pilots. Is it possible? I do not see the built-in parameters in the documentation for this.

(I can set the mustache range to max / min, but that’s not what I want. I would like to keep the mustache at the 5th and 95th percentiles).

Below is the figure I'm working on. Pay attention to the density of pilots. Boxplots

+5
python matplotlib boxplot
source share
2 answers

plt.boxplot() returns a dictionary where the fliers key contains the top and bottom flyers as line2d objects. You can manipulate them before doing the following:

Only on matplotlib> = 1.4.0

 bp = plt.boxplot(data, whis=[5, 95], showfliers=True) # Get a list of Line2D objects, representing a single line from the # minimum to the maximum flier points. fliers = bp['fliers'] # Iterate over it! for fly in fliers: fdata = fly.get_data() fly.set_data([fdata[0][0],fdata[0][-1]],[fdata[1][0],fdata[1][-1]]) 

In older versions

If you are using an older version of matplotlib, the flyers for each box are represented by two lines, not one. Thus, the loop will look something like this:

 import numpy as np for i in range(len(fliers)): fdata = fliers[i].get_data() # Get the index of the maximum y in data if # i is 0 or even, else get index of minimum y. if i%2 == 0: id = np.where(fdata[1] == fdata[1].max())[0][0] else: id = np.where(fdata[1] == fdata[1].min())[0][0] fliers[i].set_data([fdata[0][id], fdata[1][id]]) 

Also note that the showfliers argument showfliers not exist in matplotlib <1.4x, and the whisk argument whisk not accept lists.

Of course (for simple applications) you can draw a box without pilots and add maximum and minimum points to the graph:

 bp = plt.boxplot(data, whis=[5, 95], showfliers=False) sc = plt.scatter([1, 1], [data.min(), data.max()]) 

where [1, 1] is the x-position of the points.

+3
source share
 fliers = bp['fliers'] for i in range(len(fliers)): # iterate through the Line2D objects for the fliers for each boxplot box = fliers[i] # this accesses the x and y vectors for the fliers for each box box.set_data([[box.get_xdata()[0],box.get_xdata()[0]],[np.min(box.get_ydata()),β€Œβ€‹np.max(box.get_ydata())]]) # note that you can use any two values from the xdata vector 

The resulting figure showing only max and minimum flyers: enter image description here

+1
source share

All Articles