Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to draw Box Diagram by Matplotlib in Python

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the Python Matplotlib how to draw the box line map, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Introduction of Box Diagram

Box chart (Box-plot), also known as box chart or box chart, is a statistical chart used to show the dispersion of a group of data. it can show the upper bound, lower bound, median, upper and lower quartile and outliers of a group of data. The composition and meaning of the box diagram are shown in the following figure.

The key term describes the quartile:

Quartile: a group of data is arranged in order from small to large, and then divided into four equal parts. the number at the three points of division is the quartile.

First quartile (Q1): also known as "lower quartile" or "lower quartile", equal to the 25% of all the values in the sample from smallest to largest, Q1 position = 1 + (nMul) x 0.25

Second quartile (Q2): also known as the "median", equal to the 50% of all the values in the sample from small to large, the position of Q2 = 1 + (nmur1) x 0.5

The third quartile (Q3), also known as the "larger quartile" or "upper quartile", is equal to 75% of all the values in the sample from smallest to largest. Position of Q3 = 1 + (nmur1) x 0.75

Quartile spacing (InterQuartile Range,IQR): the difference between the third quartile and the first quartile (Q3 data-Q1 data)

Upper limit of Whiske (abnormal value is greater than this value): Q3 + 1.5*IQR, (1.5 indicates the excess ratio and is a coefficient, which can be adjusted according to the actual situation)

Whisker lower bound (less than this value is an exception): Q1 number-1.5*IQR.

First of all, sort this set of data to get: [12, 30, 36, 40, 45, 50, 80], the array length n is 7.

The position of Q1 = 1 + (nmur1) x 0.25 + 6 + 0.25 = 2.5, so the value of Q1 is: 30 + (36-30) * 0.5 = 33

The position of Q2 = 1 + (nmur1) x 0.5cm 1 + 6cm 0.5 = 4, so the value of Q2 is 40

The position of Q3 = 1 + (nmur1) x 0.75 (1) + 6 (0.75) = 5.5, so the value of Q3 is: 45 + (50-45) * 0.5cm 47.5

Example 2: a set of data [12, 45, 30, 80, 36, 50, 40, 43] to find out Q1, Q2, Q3 respectively

First of all, sort this set of data to get: [12, 30, 36, 40, 43, 45, 50, 80], the array length n is 8.

The position of Q1 = 1 + (nMuth1) x 0.25 million 1 + 7 percent 0.25 = 2.75, so the value of Q1 is: 30 + (36-30) * 0.75 = 34.5

The position of Q2 is = 1 + (nmur1) x 0.5: 1 + 7: 0.5 = 4.5, so the value of Q2 is 40 + (43-40) * 0.5: 41.5.

The position of Q3 = 1 + (nmur1) x 0.75 + 0.75 = 6.25, so the value of Q3 is: 45 + (50-45) * 0.25046.25

The quantile () function is provided in numpy, which can directly get the quartile. For example, np.quantile (x, 0.25) can get the Q1 value in the array x.

The method of drawing box diagram in Matplotlib: boxplotboxplot (x, notch=None, sym=None, vert=None, whis=None, positions=None, widths=None, patch_artist=None, bootstrap=None, usermedians=None, conf_intervals=None, meanline=None, showmeans=None, showcaps=None, showbox=None, showfliers=None, boxprops=None, labels=None, flierprops=None, medianprops=None, meanprops=None, capprops=None, whiskerprops=None, manage_ticks=True, autorange=False, zorder=None, *, data=None):

The meaning of the key parameters is as follows:

X: specify the data to draw the box line diagram, which can be a set of data or multiple sets of data

Notch: whether to display the box chart in the form of notches. The default is not notches.

Sym: specifies the shape of the outlier, which is shown by the blue + sign by default

Vert: whether the box chart needs to be placed vertically. The default is vertical.

Whis: specifies the distance between the upper and lower quartile and the upper and lower quartile. The default is 1.5 times the quartile difference.

Positions: specifies the location of the box chart. The default is range (1, Number1), and N is the number of box charts.

Widths: specifies the width of the box line diagram. Default is 0.5.

Patch_artist: whether to fill the color of the box. Default is False.

Meanline: whether to represent the mean in the form of lines. By default, points are used.

Showmeans: whether to display the mean, not by default

Showcaps: whether to display the two lines at the top and end of the box chart, which is displayed by default

Showbox: whether to display the box of the box chart, which is displayed by default

Showfliers: whether to display outliers, default display

Boxprops: set the properties of the box, such as border color, fill color, etc.

Labels: tagging a box chart, similar to the role of a legend

Filerprops: sets the properties of outliers, such as outlier shape, size, fill color, etc.

Medianprops: sets the properties of the median, such as line type, thickness, etc.

Meanprops: sets the properties of the mean, such as point size, color, etc.

Capprops: sets the properties of the top and end lines of a box chart, such as color, weight, etc.

Whiskerprops: set the required properties, such as color, weight, type of line, etc.

Manage_ticks: whether to adapt the label position. Default is True.

Autorange: whether to automatically adjust the range. Default is False.

Program example

(1) draw a single box diagram

Import matplotlib.pyplot as pltimport numpy as npx = np.array ([12,45,30,70,36,50,40,26,38]) print (sorted (x)) a = np.quantile (x, 0.75) # numbers b = np.quantile (x, 1/4) # Lower 1/4 numbers print ("average:", np.mean (x)) # print mean print ("median:" Np.median (x)) # print median print ("top 1/4:", a) # print top 1/4 print ("bottom 1/4:" B) # print up = a + 1/4 * (a-b) # criteria for judging outliers down = b-1.5* (a-b) # criteria for judging outliers x = np.sort (x) # sort the original data shangjie = x [x

< up][-1] # 除了异常值外的最大值xiajie = x[x >

Down] [0] # minimum value print ("upper bound:", shangjie) # print upper bound print ("up:", up) print ("down:", down) print ("lower bound:", xiajie) # print lower bound plt.grid (True) # shows grid y = plt.boxplot (x, meanline=True, showmeans=True, flierprops= {"marker": "o", "markerfacecolor": "red" "markersize": 15}) # draw a box diagram Set exception point size, style, etc. Plt.show () # display diagram

Program execution effect diagram:

The output from the console is:

[12, 26, 30, 36, 38, 40, 45, 50, 70]

Average: 38.55555555555556

Median: 38.0

Top 1/4: 45.0

The next 1/4

Upper bound: 50

Up: 67.5

Down: 7.5

Lower bound: 12

(2) draw multiple box diagrams

Import matplotlib.pyplot as pltimport numpy as npx = np.random.randint (10,100, size= (5,9)) # randomly generate five rows and nine columns between print (x) # print data plt.grid (True) # display grid plt.boxplot (x, labels=list ("ABCDEFGHI"), sym= "r +", showmeans=True) # draw box chart plt.show () # display picture

Program execution effect diagram:

Note: in the picture, the red + sign indicates the outlier and the green triangle represents the average.

The print result of the console output is:

More Python [90 99 35 32 21 31 83 71 39]

[24 95 63 50 92 41 89 16 79]

[73 73 53 21 39 60 50 55 43]

[64 94 66 26 20 73 40 68 45]

[74 72 33 81 73 59 85 23 17]]

Thank you for reading this article carefully. I hope the article "how to draw a Box Diagram in Matplotlib in Python" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report