SoFunction
Updated on 2024-10-30

Python utilizes example code for drawing box plots

I. ( ) Grammar

boxplot(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)
(
    x, # Specify the data to be plotted in a box plot
    notch=True or False, # Whether the boxplot is notched or not, default is non-notched.
    sym, # str value that specifies the shape of the anomaly, defaults to a + display
    vert=True or False, # Whether the box plot needs to be placed vertically, default True Vertical Placement
    whis, # float value, specify the distance between the upper and lower whiskers and the upper and lower quartiles, default is 1.5 times the quartile difference
    bootstrap, # int value specifying the middle of the bootstrap confidence interval cut boxplot
    positions, # Specify the position of the box plot Default is [0,1,2...].
    widths, # Specify the width of the box plot, default value: 0.5
    patch_artist=True or False, # Whether to fill the color of the box Default value False no fill
    labels, # Add labels to box-and-line diagrams, similar to what a legend does
    manage_ticks=True or False, # If True, the scale positions and labels will be adjusted to match the position of the box-and-line plot. Default: True
    showmeans=True or False, # Whether to display the mean value, default: False does not display
    meanline=True or False, # Whether to represent the mean as a line, default value False to represent it as a dot
    zorder, # Sequence of box plots
    showcaps=True or False, # Whether to display the two lines at the top and the end of the box plot, default value True display
    showbox=True or False, # Whether or not to display the box of the box chart, the default value True is displayed;
    showfliers=True or False, # Whether to display the exception value, the default value True is displayed;
    boxprops, # Set the properties of the box, such as the border color, fill color, and so on;
    flierprops, # Set the properties of the exception, such as the shape, size, and fill color of the exception;
    medianprops, # Set the properties of the median, such as the type of line, thickness, etc;
    meanprops, # Set the properties of the mean, such as point size, color, etc;
    capprops, # Set the properties of the top and end lines of the box-and-line diagram, such as color, thickness, and so on;
    whiskerprops,# Set the properties of the whiskers, such as color, thickness, type of line, etc.
)

II. Drawing of box diagrams

① Draw a simple box diagram

import  as plt
import numpy as np
 
data=[(0,std,100) for std in range(1,4)]
 
(data) 
 
()

②Plotting the box shape for each parameter

(1) notch parameter (bool value, whether or not to notch the form to show the box plot, default value False non-notch)

import  as plt
 
data=[(0,std,100) for std in range(1,4)]
 
fig=()
 
# notch=True form of notch
ax1=fig.add_subplot(121)
(data,notch=True) 
('Form of notch',size=20)
 
# notch=False non-notched forms
ax2=fig.add_subplot(122)
(data,notch=False) 
('Non-notched forms',size=20)
 
()

(2) sym (str, specifies the shape of the anomaly, defaults to a + display)

import  as plt
import numpy as np
 
data=[(0,std,100) for std in range(1,4)]
 
(data,sym='^') 
 
()

(3) vert parameter (bool value, whether the box plot needs to be placed vertically, default True vertical placement)

import  as plt
 
data=[(0,std,100) for std in range(1,4)]
 
fig=()
 
# vert=True box plot placed vertically
ax1=fig.add_subplot(121)
(data,vert=True) 
('Vertical placement',size=20)
 
# vert=False box plot horizontal placement
ax2=fig.add_subplot(122)
(data,vert=False) 
('Horizontal placement',size=20)
 
()

(4) widths parameter (float value, specifies the width of the box plot, default value: 0.5)

import  as plt
import numpy as np
 
data=[(0,std,100) for std in range(1,4)]
 
(data,widths=[0.3,0.6,0.5]) 
 
()

(5) patch_artist (bool value, whether to fill the box color, default value: False not fill)

import  as plt
 
data=[(0,std,100) for std in range(1,4)]
 
fig=()
 
# patch_artist=True fill box color
ax1=fig.add_subplot(121)
(data,patch_artist=True) 
('Fill box color',size=20)
 
# patch_artist=False does not fill the box color
ax2=fig.add_subplot(122)
(data,patch_artist=False) 
('Do not fill the box color',size=20)
 
()

(6) showmeans parameter (bool value, whether to show the mean value, the default value False does not show)

import  as plt
 
data=[(0,std,100) for std in range(1,4)]
 
fig=()
 
# showmeans=True showmeans
ax1=fig.add_subplot(121)
(data,showmeans=True) 
('Show Means',size=20)
 
# showmeans=False does not show means
ax2=fig.add_subplot(122)
(data,showmeans=False) 
('Do not show mean values',size=20)
 
()

(7) meanline parameter (bool value, whether to express the mean value in the form of a line, the default value of False is expressed in dots)

Note: The effect is only visible when showmeans=True (when the mean is displayed).

import  as plt
 
data=[(0,std,100) for std in range(1,4)]
 
fig=()
 
# meanline=True Display mean values as lines
ax1=fig.add_subplot(121)
(data,showmeans=True,meanline=True) 
('Showing averages with lines',size=20)
 
# meanline=False show mean with points
ax2=fig.add_subplot(122)
(data,showmeans=True,meanline=False) 
('Showing averages with dots',size=20)
 
()

(8) showcaps parameter (bool value, whether to display the top and end of the box plot of the two lines, the default value True show)

import  as plt
 
data=[(0,std,100) for std in range(1,4)]
 
fig=()
 
# showcaps=True displays the two lines at the top and the end of the box plot
ax1=fig.add_subplot(121)
(data,showcaps=True) 
('Display',size=20)
 
# showcaps=False does not show the two lines at the top and the end of the boxplot
ax2=fig.add_subplot(122)
(data,showcaps=False) 
('Not shown',size=20)
 
()

(9) showbox parameter (bool value, whether to display the box of the box line chart, the default value True show)

import  as plt
 
data=[(0,std,100) for std in range(1,4)]
 
fig=()
 
# showbox=True show box for boxplots
ax1=fig.add_subplot(121)
(data,showbox=True) 
('Display',size=20)
 
# showbox=False does not show the box of a box plot
ax2=fig.add_subplot(122)
(data,showbox=False) 
('Not shown',size=20)
 
()

(10) showfliers parameter (bool value, whether to display abnormal values, the default value True display)

import  as plt
 
data=[(0,std,100) for std in range(1,4)]
 
fig=()
 
# showfliers=True show outliers
ax1=fig.add_subplot(121)
(data,showfliers=True) 
('Display',size=20)
 
# showfliers=False does not show outliers
ax2=fig.add_subplot(122)
(data,showfliers=False) 
('Not shown',size=20)
 
()

(11) boxprops parameters (set box properties such as border color, fill color, etc.)

import  as plt
 
data=[(0,std,100) for std in range(1,4)]
 
fig=()
 
# sboxprops={'color':'r'} set the box border color
ax1=fig.add_subplot(121)
(data,boxprops={'color':'r'}) 
('Set box border color',size=20)
 
# patch_artist=True Fill box color
# boxprops={'facecolor':'pink'} set box fill color
ax2=fig.add_subplot(122)
(data,patch_artist=True,boxprops={'facecolor':'pink'}) 
('Set box fill color',size=20)
 
()

(12) flierprops parameter (sets properties of outliers, such as shape, size, fill color of outlier, etc.)

import  as plt
import numpy as np
 
data=[(0,std,100) for std in range(1,4)]
 
(data,flierprops={'marker':'*'}) 
 
()

(13) medianprops parameter (sets properties of the median, such as type of line, thickness, etc.)

import  as plt
import numpy as np
 
data=[(0,std,100) for std in range(1,4)]
 
(data,medianprops={'linestyle':':','linewidth':5,'color':'m'}) 
 
()

(14) meanprops parameter (sets the properties of the mean)

import  as plt
import numpy as np
 
data=[(0,std,100) for std in range(1,4)]
 
(data,showmeans=True,meanprops={'marker':'*'})
 
()

(15) capprops parameter (sets the properties of the top and end lines of the box-and-line diagram, such as color, thickness, etc.)

import  as plt
import numpy as np
 
data=[(0,std,100) for std in range(1,4)]
 
(data,
            showmeans=True,
            capprops={'linestyle':'--','color':'m','linewidth':3})
 
()

(16) whiskerprops parameter (sets whisker properties such as color, thickness, type of line, etc.)

import  as plt
import numpy as np
 
data=[(0,std,100) for std in range(1,4)]
 
(data,
            showmeans=True,
            whiskerprops={'linestyle':'--','color':'m','linewidth':3})
 
()

summarize

to this article on the use of Python () to draw a box plot of the article is introduced to this, more related () to draw a box plot content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future more!