bf4785d2创建于 2022年9月19日历史提交

Box Plot

In descriptive statistics, a box plot or boxplot is a method for graphically demonstrating the locality, spread and skewness groups of numerical data through their quartiles. In addition to the box on a box plot, there can be lines (which are called whiskers) extending from the box indicating variability outside the upper and lower quartiles, thus, the plot is also termed as the box-and-whisker plot and the box-and-whisker diagram.

Find more information about box plot in Wikipedia.

The input x can be either a vector or a matrix. When x is a matrix, each row of x is considered a different set of data. Alternatively, one can also provide parameter groups to separate x into different groups.

Methods

====================== ================================= Function Description ====================== ================================= boxplot 3 methods Plot a box chart with x. ====================== =================================

Description ^^^^^^^^^^^

.. function:: boxplot(x)

:param x: Vector<Float64>
:return: BoxChart

Plot a box chart with vector x.

.. function:: boxplot(x)

:param x: Matrix<Float64>
:return: BoxChart

Plot a box chart with matrix x.

.. function:: boxplot(x, groups)

:param x: Vector<Float64>
:param groups: Array<String>
:return: BoxChart

Plot a box chart with vector x, where data is separated into multiple groups.

Examples

  1. Basic boxplot with a single set of data.

::

let mpg = vector<Float64>([
    18.0, 15.0, 18.0, 16.0, 17.0, 15.0, 14.0, 14.0, 14.0, 15.0, 15.0, 14.0,
    15.0, 14.0, 24.0, 22.0, 18.0, 21.0, 27.0, 26.0, 25.0, 24.0, 25.0, 26.0,
    21.0, 10.0, 10.0, 11.0,  9.0, 28.0, 25.0, 25.0, 26.0, 27.0, 17.5, 16.0,
    15.5, 14.5, 22.0, 22.0, 24.0, 22.5, 29.0, 24.5, 29.0, 33.0, 20.0, 18.0,
    18.5, 17.5, 29.5, 32.0, 28.0, 26.5, 20.0, 13.0, 19.0, 19.0, 16.5, 16.5,
    13.0, 13.0, 13.0, 28.0, 27.0, 34.0, 31.0, 29.0, 27.0, 24.0, 23.0, 36.0,
    37.0, 31.0, 38.0, 36.0, 36.0, 36.0, 34.0, 38.0, 32.0, 38.0, 25.0, 38.0,
    26.0, 22.0, 32.0, 36.0, 27.0, 27.0, 44.0, 32.0, 28.0, 31.0])
boxplot(mpg)
xlabel("All Vehicles")
ylabel("Miles per Gallon (MPG)")
title("Miles per Gallon for All Vehicles")

Result:

.. image:: ../../../tests/imgs/boxplot/boxchart_1.png :align: center :width: 360

  1. Boxplot with data separated into different groups.

::

let mpg = vector<Float64>([
    18.0, 15.0, 18.0, 16.0, 17.0, 15.0, 14.0, 14.0, 14.0, 15.0, 15.0, 14.0,
    15.0, 14.0, 24.0, 22.0, 18.0, 21.0, 27.0, 26.0, 25.0, 24.0, 25.0, 26.0,
    21.0, 10.0, 10.0, 11.0,  9.0, 28.0, 25.0, 25.0, 26.0, 27.0, 17.5, 16.0,
    15.5, 14.5, 22.0, 22.0, 24.0, 22.5, 29.0, 24.5, 29.0, 33.0, 20.0, 18.0,
    18.5, 17.5, 29.5, 32.0, 28.0, 26.5, 20.0, 13.0, 19.0, 19.0, 16.5, 16.5,
    13.0, 13.0, 13.0, 28.0, 27.0, 34.0, 31.0, 29.0, 27.0, 24.0, 23.0, 36.0,
    37.0, 31.0, 38.0, 36.0, 36.0, 36.0, 34.0, 38.0, 32.0, 38.0, 25.0, 38.0,
    26.0, 22.0, 32.0, 36.0, 27.0, 27.0, 44.0, 32.0, 28.0, 31.0])

let origin = [
    "USA",     "USA",     "USA",     "USA",   "USA",   "USA",     "USA",
    "USA",     "USA",     "USA",     "USA",   "USA",   "USA",     "USA",
    "Japan",   "USA",     "USA",     "USA",   "Japan", "Germany", "France",
    "Germany", "Sweden",  "Germany", "USA",   "USA",   "USA",     "USA",
    "USA",     "Italy",   "Germany", "USA",   "USA",   "France",  "USA",
    "USA",     "USA",     "USA",     "USA",   "USA",   "USA",     "USA",
    "USA",     "USA",     "Germany", "Japan", "USA",   "USA",     "USA",
    "USA",     "Germany", "Japan",   "Japan", "USA",   "Sweden",  "USA",
    "France",  "Japan",   "Germany", "USA",   "USA",   "USA",     "USA",
    "USA",     "USA",     "USA",     "USA",   "USA",   "USA",     "USA",
    "USA",     "Germany", "Japan",   "Japan", "USA",   "USA",     "Japan",
    "Japan",   "Japan",   "Japan",   "Japan", "Japan", "USA",     "USA",
    "USA",     "USA",     "Japan",   "USA",   "USA",   "USA",     "Germany",
    "USA",     "USA",     "USA"
]

boxplot(mpg, origin)
xlabel("All Vehicles")
ylabel("Miles per Gallon (MPG)")
title("Miles per Gallon for All Vehicles")

Result:

.. image:: ../../../tests/imgs/boxplot/boxchart_2.png :align: center :width: 360

  1. This example demonstrate the case when the input is a matrix. Also compare different box styles.

::

var m: Random = Random(0)
let x = randn(m, 25, 100, 0.0, 1.0)

subplot(2,1,0)
boxplot(x)

subplot(2,1,1)
let box = boxplot(x)
box.box_style(outline)

Result:

.. image:: ../../../tests/imgs/boxplot/boxchart_3.png :align: center :width: 360