3fe73e77创建于 2022年9月16日历史提交

Bar Plot

A bar chart or bar graph is a chart or graph that presents categorical data with rectangular bars with heights or lengths proportional to the values that they represent. The bars can be plotted vertically or horizontally. A vertical bar chart is sometimes called a column chart.

Find more information about bar chart in Wikipeida.

Methods

=========================== ================================ Function Description =========================== ================================ bar() 6 functions Plot an bar chart. barstacked() 3 functions Plot bar chart and stack them. =========================== ================================

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

.. function:: bar(y)

:param y: Vector<Float64>
:return: Bars

Plot a bar chart.

.. function:: bar(x, y)

:param x: Vector<Float64>
:param y: Vector<Float64>
:return: Bars

Plot a bar chart with coordinate axis x.

.. function:: bar(y, size)

:param y: Vector<Float64>
:param size: Float64
:return: Bars

Plot a bar chart with bar size.

.. function:: bar(Y)

:param y: Matrix<Float64>
:return: Bars

Plot a bar chart with some groups.

.. function:: bar(x, Y)

:param x: Vector<Float64>
:param Y: Matrix<Float64>
:return: Bars

Plot a bar chart with some groups and coordinate axis x of every group. 

.. function:: bar(axes, Y)

:param axes: AxesType
:param Y: Matrix<Float64>
:return: Bars

Plot a bar chart in given axes.

.. function:: barstacked(Y)

:param Y: Matrix<Float64>

Plot a stacked bar chart.

.. function:: barstacked(x, Y)

:param x: Vector<Float64>
:param axes: AxesType
:param y: Matrix<Float64>

Plot a stacked bar chart with coordinate axis x.

.. function:: barstacked(axes, Y)

:param axes: AxesType
:param Y: Matrix<Float64>

Plot a stacked bar chart in given axes.

Examples

  1. Basic bar plot (providing only :math:y values).

::

let y = vector<Float64>([75.0, 91.0, 105.0, 123.5,
                        131.0, 150.0, 179.0, 203.0,
                        226.0, 249.0, 281.5])
bar(y)

Result:

.. image:: ../../../tests/imgs/bar_plot/bar_1.png :align: center :width: 360

  1. Basic bar plot (providing :math:x,y values).

::

let x = linspace(1900.0, 2000.0, num: 11)
let y = vector<Float64>([75.0, 91.0, 105.0, 123.5,
                        131.0, 150.0, 179.0, 203.0,
                        226.0, 249.0, 281.5])
bar(x, y)

Result:

.. image:: ../../../tests/imgs/bar_plot/bar_2.png :align: center :width: 360

  1. Bar plot with custom width (providing :math:y,width values).

::

let y = vector<Float64>([75.0, 91.0, 105.0, 123.5,
                        131.0, 150.0, 179.0, 203.0,
                        226.0, 249.0, 281.5])
bar(y, 0.4)

Result:

.. image:: ../../../tests/imgs/bar_plot/bar_3.png :align: center :width: 360

  1. Basic bar plot (providing :math:y values as a matrix).

::

let Y = matrix([[2.0, 2.0, 2.0, 2.0],
                [2.0, 5.0, 8.0, 11.0],
                [3.0, 6.0, 9.0, 12.0]])
bar(Y)

Result:

.. image:: ../../../tests/imgs/bar_plot/bar_4.png :align: center :width: 360

  1. Basic barstacked plot (providing :math:y values as a matrix).

::

let Y = matrix([[2.0, 2.0, 2.0, 2.0],
                [2.0, 5.0, 8.0, 11.0],
                [3.0, 6.0, 9.0, 12.0]])
barstacked(Y)

Result:

.. image:: ../../../tests/imgs/bar_plot/bar_5.png :align: center :width: 360

  1. Basic barstacked plot (providing :math:x,y values).

::

let x = vector<Float64>([1980.0, 1990.0, 2000.0])
let Y = matrix([[15.0, 10.0, -10.0],
                [20.0, -17.0, 5.0],
                [-5.0, 21.0, 15.0]])
barstacked(x, Y)

Result:

.. image:: ../../../tests/imgs/bar_plot/bar_6.png :align: center :width: 360

  1. Bar plot with custom ticklabels.

::

let y = vector<Float64>([10.0, 21.0, 33.0, 52.0])
bar(y)
gca().x_axis().ticklabels(["Small", "Medium", "Large", "Extra Large"])

Result:

.. image:: ../../../tests/imgs/bar_plot/bar_7.png :align: center :width: 360

  1. Bar plot with custom labels.

::

let x = vector<Float64>([1.0, 2.0, 3.0])
let y = matrix([[2.0, 3.0, 6.0],
                [11.0, 23.0, 26.0]])
let b = bar(x, y)

var label_x = ArrayList<Float64>()
var label_y = ArrayList<Float64>()
var labels = ArrayList<String>()
let x_size = x.size()
let y_size = y.getRows()
for (i in 0..y_size) {
    for (j in 0..x_size) {
        label_x.append(b.x_end_point(i, j))
        label_y.append(y[i, j] + 1.0)
        labels.append(y[i, j].toString())
    }
}

hold(true)
text(label_x, label_y, labels)

Result:

.. image:: ../../../tests/imgs/bar_plot/bar_8.png :align: center :width: 360

  1. Two bar plots in a tiled layout.

::

let y = matrix([[1.0, 4.0],
                [2.0, 5.0],
                [3.0, 6.0]])

tiledlayout(2, 1)

let ax1 = nexttile()
bar(ax1, y)

let ax2 = nexttile()
barstacked(ax2, y)

Result:

.. image:: ../../../tests/imgs/bar_plot/bar_9.png :align: center :width: 360

  1. Bar plot with custom face color. (red)

::

let y = vector<Float64>([75.0, 91.0, 105.0, 123.5,
                        131.0, 150.0, 179.0, 203.0,
                        226.0, 249.0, 281.5])

bar(y).face_color("r")

Result:

.. image:: ../../../tests/imgs/bar_plot/bar_10.png :align: center :width: 360

  1. Bar plot with custom face color, edge color, line width.

::

let y = vector<Float64>([75.0, 91.0, 105.0, 123.5,
                        131.0, 150.0, 179.0, 203.0,
                        226.0, 249.0, 281.5])

bar(y).face_color(0.0, 0.5, 0.5).edge_color(0.0, 0.9, 0.9).line_width(1.5)

Result:

.. image:: ../../../tests/imgs/bar_plot/bar_11.png :align: center :width: 360

  1. Bar plot with custom ticks.

::

let x1 = concat(vector<Float64>([1.0]), linspace(3.0, 10.0, num: 8))
var r: Random = Random(0)
bar(x1, rand(r, 9, 0.0, 1.0))

hold(true)
bar(vector<Float64>([2.0]), rand(r, 1, 0.0, 1.0))
gca().x_axis().tick_values(linspace(1.0, 10.0, num: 10))

Result:

.. image:: ../../../tests/imgs/bar_plot/bar_12.png :align: center :width: 360

  1. Bar plot with three axes, setting custom face color on the third axes.

::

let y = matrix(
    [[10.0, 30.0, 50.0],
    [15.0, 35.0, 55.0],
    [20.0, 40.0, 62.0]])

let b = bar(y)
b.face_colors(2, 0.0, 0.2, 0.6, 0.5)
gcf().draw()

Result:

.. image:: ../../../tests/imgs/bar_plot/bar_13.png :align: center :width: 360