plot::Bars2d

2D bar chart

MuPAD® notebooks will be removed in a future release. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.

Syntax

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

Description

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

plot::Bars2d([ a1, a2, ...]) creates a bar chart with bars of height a1, a2, …

With plot::Bars2d([[ a1, a2,...], [ b1, b2,...], ...]), bars are plotted in the order a1, b1, …, a gap, a2, b2, … Cf. Example 2.

The horizontal positions and the widths of the bars may be controlled by the attributes BarCenters and BarWidths, respectively.

The attribute GroupStyle provides grouping options.

Attributes

AttributePurposeDefault Value
AffectViewingBoxinfluence of objects on the ViewingBox of a sceneTRUE
AntiAliasedantialiased lines and points?FALSE
BarStyledisplay style of bar plotsBoxes
BarWidthswidths of bars[[1.0]]
BarCentersposition of bars 
Colorthe main color 
Colorslist of colors to use[RGB::Blue, RGB::Red, RGB::Green, RGB::MuPADGold, RGB::Orange, RGB::Cyan, RGB::Magenta, RGB::LimeGreen, RGB::CadmiumYellowLight, RGB::AlizarinCrimson, RGB::Aqua, RGB::Lavender, RGB::SeaGreen, RGB::AureolineYellow, RGB::Banana, RGB::Beige, RGB::YellowGreen, RGB::Wheat, RGB::IndianRed, RGB::Black]
Datathe (statistical) data to plot 
DrawModeorientation of boxes and barsVertical
Filledfilled or transparent areas and surfacesTRUE
FillPatternslist of area fill types[Solid]
Framesthe number of frames in an animation50
GroupStylegrouping options in 2D bar plotsMultipleBars
Legendmakes a legend entry 
LegendTextshort explanatory text for legend 
LegendEntryadd this object to the legend?FALSE
LineColorcolor of linesRGB::Black
LineWidthwidth of lines0.35
LineStylesolid, dashed or dotted lines?Solid
LinesVisiblevisibility of linesTRUE
Namethe name of a plot object (for browser and legend) 
ParameterEndend value of the animation parameter 
ParameterNamename of the animation parameter 
ParameterBegininitial value of the animation parameter 
ParameterRangerange of the animation parameter 
PointSizethe size of points1.5
PointStylethe presentation style of pointsFilledCircles
Shadowsdisplay “shadows” for bar plots?FALSE
TimeEndend time of the animation10.0
TimeBeginstart time of the animation0.0
TimeRangethe real time span of an animation0.0 .. 10.0
Titleobject title 
TitleFontfont of object titles[" sans-serif ", 11]
TitlePositionposition of object titles 
TitleAlignmenthorizontal alignment of titles w.r.t. their coordinatesCenter
TitlePositionXposition of object titles, x component 
TitlePositionYposition of object titles, y component 
VisiblevisibilityTRUE
VisibleAfterobject visible after this time value 
VisibleBeforeobject visible until this time value 
VisibleFromToobject visible during this time range 
VisibleAfterEndobject visible after its animation time ended?TRUE
VisibleBeforeBeginobject visible before its animation time starts?TRUE

Examples

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]))

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]]))

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))

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))

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))

Example 4

We demonstrate alternative grouping styles:

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

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

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)

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))

plot(plot::Bars2d(M))

plot(plot::Bars2d(A))

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)).
                    " megatons   " $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 = "megatons", 
     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 = "Capacities in megatons",
     HeaderFont = ["Arial", 12, Bold],
     Footer = "\n\nAmount of waste taken into account:\n".datasums,
     FooterFont = ["Arial", 8],
     FooterAlignment = Left,

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

plot(S1)

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("missing capacities", [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("planned and potential capacities", [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("known capacities", [14, 23],
               HorizontalAlignment = Left, 
               TextFont = ["Arial", 8])
):

plot(S2, BorderWidth = 0.2)

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)

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

Parameters

a1, a2, …, b1, b2, …, …

Real-valued expressions, possibly in the animation parameter.

a1, a2, …, b1, b2, …, … is equivalent to the attribute Data.

a

Animation parameter, specified as a = amin..amax, where amin is the initial parameter value, and amax is the final parameter value.

See Also

MuPAD Functions

MuPAD Graphical Primitives