Python中的 Matplotlib.axes.Axes.bxp()
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 Axes 类包含大部分图形元素:Axis、Tick、Line2D、Text、Polygon 等,并设置坐标系。 Axes 的实例通过回调属性支持回调。
matplotlib.axes.Axes.bxp()函数
matplotlib 库的 axes 模块中的Axes.bxp()函数用于为 x 的每一列或序列 x 中的每个向量制作箱须图。
Syntax: Axes.bxp(self, bxpstats, positions=None, widths=None, vert=True, patch_artist=False, shownotches=False, showmeans=False, showcaps=True, showbox=True, showfliers=True, boxprops=None, whiskerprops=None, flierprops=None, medianprops=None, capprops=None, meanprops=None, meanline=False, manage_ticks=True, zorder=None)
Parameters: This method accept the following parameters that are described below:
- bxpstats : This parameter is alist of dictionaries containing stats for each boxplot.
- positions : This parameter is used to sets the positions of the violins.
- vert: This parameter is an optional parameter and contain boolean value. It makes the vertical violin plot if true.Otherwise horizontal.
- widths: This parameter is used to sets the width of each violin either with a scalar or a sequence.
- patch_artist : This parameter is used to produce boxes with the Line2D artist if it is false. Otherwise, boxes with Patch artists.
- manage_ticks : This parameter is used to adjust the tick locations and labels.
- zorder : This parameter is used to sets the zorder of the boxplot.
- shownotches: This parameter contain boolean value. It is used to produce a notched and rectangular box plot.
- showmeans : This parameter contain boolean value. It is used to toggle rendering of the means.
- showcaps : This parameter contain boolean value. It is used to toggle rendering of the caps.
- showfliers : This parameter contain boolean value. It is used to toggle rendering of the fliers.
- boxprops : This parameter is used to set the plotting style of the boxes.
- whiskerprops : This parameter is used to set the plotting style of the whiskers.
- capprops : This parameter is used to set the plotting style of the caps.
- flierprops : This parameter is used to set the plotting style of the fliers.
- medianprops : This parameter is used to set the plotting style of the medians.
- meanprops : This parameter is used to set the plotting style of the means.
Returns: This returns the following:
- result :This returns the dictionary which maps each component of the violinplot to a list of the matplotlib.lines.Line2D instances.
下面的示例说明了 matplotlib.axes 中的 matplotlib.axes.Axes.bxp()函数:
示例 1:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
np.random.seed(10**7)
data = np.random.lognormal(size =(10, 4),
mean = 4.5,
sigma = 4.75)
labels = ['G1', 'G2', 'G3', 'G4']
result = cbook.boxplot_stats(data,
labels = labels,
bootstrap = 1000)
for n in range(len(result)):
result[n]['med'] = np.median(data)
result[n]['mean'] *= 0.1
fig, axes1 = plt.subplots()
axes1.bxp(result)
axes1.set_title('matplotlib.axes.Axes.bxp() Example')
plt.show()
输出:
示例 2:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
np.random.seed(10**7)
data = np.random.lognormal(size =(37, 4),
mean = 4.5,
sigma = 1.75)
labels = ['G1', 'G2', 'G3', 'G4']
stats = cbook.boxplot_stats(data, labels = labels,
bootstrap = 100)
for n in range(len(stats)):
stats[n]['med'] = np.median(data)
stats[n]['mean'] *= 2
fig, [axes1, axes2, axes3] = plt.subplots(nrows = 1,
ncols = 3,
sharey = True)
axes1.bxp(stats)
axes2.bxp(stats, showmeans = True)
axes3.bxp(stats, showmeans = True, meanline = True)
axes2.set_title('matplotlib.axes.Axes.bxp() Example')
plt.show()
输出: