Skip to Main Content Skip to Search
Product Documentation

plot::Bars2d2D bar chart

plot::Bars2d([[a1, a2, ...], [b1, b2, ...], ...]) generates a bar chart with bar heights a1, b1, ..., a2, b2, ....

→ Examples

Calls:

plot::Bars2d([[a1, a2, ...], [b1, b2, ...], ...], <a = amin .. amax>, Options)

plot::Bars2d([a1, a2, ...], <a = amin .. amax>, Options)

Parameters:

a1, a2, ..., b1, b2, ..., ...: 

real-valued expressions, possibly in the animation parameter.
a1, a2, ..., b1, b2, ..., ... is equivalent to the attribute Data.

Related Domains:

plot::Bars3d, plot::Histogram2d, plot::Scatterplot

Related Functions:

plot, plot::copy

Details:

Example 1

Given a single list of values, plot::Bars2d plots bars of the corresponding height, filled solidly in one color:

plot(plot::Bars2d([1, 2, 3, 4, 5]))

MuPAD graphics

Example 2

When asked to plot a list of lists of values, plot::Bars2d will group the first entries of all lists, the second entries and so on, with a small gap between the groups:

plot(plot::Bars2d([[ 5, 10, 24, -3],
                   [ 6,  5,  2, 18],
                   [19, 45, 12,-10]]))

MuPAD graphics

Example 3

The appearance of the plots can be controlled with a number of attributes. For example, Colors accepts a list of colors for the bars and Shadows switches on “shadows,” giving a slight impression of depth:

plot(plot::Bars2d([[ 5, 10, 24,  -3],
                   [ 6,  5,  2,  18],
                   [19, 45, 12, -10]],
                  Colors = [RGB::Red, RGB::Green, RGB::Blue],
                  Shadows = TRUE))

MuPAD graphics

Using DrawMode, plot::Bars2d can be made to draw horizontal bars instead of vertical ones:

plot(plot::Bars2d([[ .5, 1.0, 2.4,  -.3],
                   [ .6,  .5,  .2,  1.8],
                   [1.9, 4.5, 1.2, -1.0]],
                  Colors = [RGB::Red,RGB::Green,RGB::Blue],
                  Shadows = TRUE, DrawMode = Horizontal))

MuPAD graphics

BarStyle is used to plot points or lines instead of rectangles:

plot(plot::Bars2d([[ 5, 10, 24,  -3],
                   [ 6,  5,  2,  18],
                   [19, 45, 12, -10]], BarStyle = Lines))

MuPAD graphics

Example 4

We demonstrate alternative grouping styles:

plot(plot::Bars2d([[ 5, 10, 15, 20],
                   [ 6,  5,  4,  3],
                   [10,  5,  3,  1]], GroupStyle = MultipleBars))

MuPAD graphics

plot(plot::Bars2d([[ 5, 10, 15, 20],
                   [ 6,  5,  4,  3],
                   [10,  5,  3,  1]], GroupStyle = SingleBars))

MuPAD graphics

Example 5

To plot a single group of data with different colors, they must be placed in individual lists:

plot(plot::Bars2d([[binomial(15,i)] $ i = 0..15],
                  Colors = [[1-j/15, j/15, 0.9] $ j = 0..15]),
            XAxisVisible)

MuPAD graphics

Example 6

plot::Bars2d accepts input in form of lists (as above), as a matrix, or as a one- or two-dimensional array:

L := [ [2,  1,  2, 4, 5],
       [1,  2,  3, 2, 1],
       [2, -1, -3, 1, 2],
       [5,  4,  3, 2, 1],
       [2,  1,  2, 1, 2]]:
M := matrix(L):
A :=array(1..5, 1..5,
          (1,1) = 2, (1,2) = 1, (1,3) =  2, (1,4) = 4, (1,5) = 5,
          (2,1) = 1, (2,2) = 2, (2,3) =  3, (2,4) = 2, (2,5) = 1,
          (3,1) = 2, (3,2) = 1, (3,3) = -3, (3,4) = 1, (3,5) = 2,
          (4,1) = 5, (4,2) = 4, (4,3) =  3, (4,4) = 2, (4,5) = 1,
          (5,1) = 2, (5,2) = 1, (5,3) =  2, (5,4) = 1, (5,5) = 2):
plot(plot::Bars2d(L))

MuPAD graphics

plot(plot::Bars2d(M))

MuPAD graphics

plot(plot::Bars2d(A))

MuPAD graphics

Example 7

Here is a real life example of a bar plot taken from a German magazine. It visualizes data related to waste management. We reproduce the plot via MuPAD®. The main ingredient is a bar plot generated via plot::Bars2d with the option GroupStyle = SingleBars. Generating the annotations is somewhat tricky:

data := [[25  , 24.6, 30.8 ],
         [ 2  ,  2.8, 11   ],
         [ 7.1,  3.3,  4.05]]:

sw := 1.5:
bw := 2.0:
n := nops(data):
w := sw + bw:
myticks := [(i-1)* w + sw + bw/2 $ i = 1..n]:

m := nops(data[1]):

datalabels := ["Prognos", "LAGA", "BDE"]:

// cumulative data for the groups
datasums := _concat(datalabels[i].": ".
                    expr2text(_plus(data[j][i]$j=1..m)).
                    " Mio. t   " $i=1..n):

// generate a list of text objects containing the data values
// and place them in the centers of the bars:
datatext := []:
for i from 1 to n do
  h := 0;
  for j from 1 to m do
    d := data[j][i];
    datatext := datatext, plot::Text2d(expr2text(d),
                                       [myticks[i], h + d/2],
                                       TextFont = [8, RGB::White],
                                       VerticalAlignment = Center,
                                       HorizontalAlignment = Center);
    h := h + d
  end
end:

Here is the bar plot with the annotations. Many scene options are used to fine tune the graphics:

S1:=plot::Scene2d(
    plot::Bars2d(data,
                 Colors=[RGB::LimeGreen, RGB::Blue, RGB::Red],
                 GroupStyle = SingleBars,
                 BarCenters = [myticks[i] $ i=1..n],
                 BarWidths = [[bw]],
                 DrawMode = Vertical),

     // scene options:

     ViewingBox = [0 .. w*n + sw, 0 .. 50],

     // options for the grid
     XGridVisible = FALSE,
     YGridVisible = TRUE,
     XSubgridVisible = FALSE,
     YSubgridVisible = TRUE,
     GridLineColor = RGB::DarkGrey,
     SubgridLineColor = RGB::DarkGrey,

     // options for the axes
     Axes = Boxed,
     AxesTips = FALSE,
     AxesInFront = TRUE,
     AxesTitleFont = ["Arial", 12, Bold],
     XAxisVisible = TRUE,
     YAxisTitleOrientation = Vertical,
     YAxisTitleAlignment = Center,
     YAxisTitle = "Mio. t",
     XAxisTitle = "",

     // options for the ticks along the axes
     TicksLabelFont = ["Arial", 10],
     XTicksVisible = FALSE,
     XTicksNumber = None,
     XTicksAt = [myticks[i] = datalabels[i] $ i=1..n],

     // layout
     RightMargin = 50,

     // annotation
     datatext,

     // header and footer
     Header = "Kapazitäten in Mio. t",
     HeaderFont = ["Arial", 12, Bold],
     Footer = "\n\nBerücksichtigte Abfallmengen:\n".datasums,
     FooterFont = ["Arial", 8],
     FooterAlignment = Left,

     // use a yellowish background
     BackgroundColor = [0.886275, 0.870588, 0.294118]
):

plot(S1)

MuPAD graphics

Next, we build a legend made of colored rectangles and text objects:

S2 := plot::Scene2d(
  ViewingBox = [0..20, 0..50],
  Axes = None,
  plot::Rectangle(13..13.5, 35..36,
                  Filled = TRUE,
                  FillPattern = Solid,
                  FillColor = RGB::Red,
                  LineColor = RGB::Black),
  plot::Text2d("fehlende Kapazitäten\n(Entsorgungslücke)", [14, 35],
               HorizontalAlignment = Left,
               TextFont = ["Arial", 8]),
  plot::Rectangle(13..13.5, 29..30,
                  Filled = TRUE,
                  FillPattern = Solid,
                  FillColor = RGB::Blue,
                  LineColor = RGB::Black),
  plot::Text2d("geplante und potentielle\nKapazitäten", [14, 29],
               HorizontalAlignment = Left, TextFont = ["Arial", 8]),
  plot::Rectangle(13..13.5, 23..24,
                  Filled = TRUE,
                  FillPattern = Solid,
                  FillColor = RGB::Green,
                  LineColor = RGB::Black),
  plot::Text2d("sichere Kapazitäten", [14, 23],
               HorizontalAlignment = Left,
               TextFont = ["Arial", 8])
):

plot(S2, BorderWidth = 0.2)

MuPAD graphics

The final picture consists of the bar plot S1 and the legend S2. We just put S2 on top of S1, making the background of S2 transparent:

S1::Width := 1: S1::Height := 1:
S2::Width := 1: S2::Height := 1:
S1::Bottom := 0: S1::Left := 0:
S2::Bottom := 0: S2::Left := 0:
S1::BackgroundTransparent := FALSE:
S2::BackgroundTransparent := TRUE:

plot(S1, S2, Layout = Relative)

MuPAD graphics

delete data, datalabels, datasums, datatext, myticks,
       sw, bw, n, m, w, i, h, j, d, S1, S2:

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS