Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

bar

Syntax

bar(y)
bar(x,y)
bar(___,width)
bar(___,style)
bar(___,color)
bar(___,Name,Value)
bar(ax,___)
b = bar(___)

Description

example

bar(y) creates a bar graph with one bar for each element in y. If y is a matrix, then bar groups the bars according to the rows in y.

example

bar(x,y) draws the bars at the locations specified by x.

example

bar(___,width) sets the relative bar width, which controls the separation of bars within a group. Specify width as a scalar value. Use this option with any of the input argument combinations in the previous syntaxes.

example

bar(___,style) specifies the style of the bar groups. For example, use 'stacked' to display each group as one multicolored bar.

example

bar(___,color) sets the color for all the bars. For example, use 'r' for red bars.

example

bar(___,Name,Value) modifies the bar chart using one or more name-value pair arguments. The settings apply to all bars plotted. For example, use 'EdgeColor','black' to outline all the bars in black.

example

bar(ax,___) plots into the axes specified by ax instead of into the current axes (gca). The option ax can precede any of the input argument combinations in the previous syntaxes.

example

b = bar(___) returns one or more Bar objects. If y is a vector, then bar creates one Bar object. If y is a matrix, then bar creates a Bar object for each column. Use b to make future modifications to the Bar objects after they are created.

Examples

collapse all

y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
bar(y)

Specify the bar locations along the x-axis.

x = 1900:10:2000;
y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
bar(x,y)

Set the width of each bar to 40 percent of the total space available for each bar.

y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
bar(y,0.4)

Display four groups of three bars.

y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
bar(y)

Display one bar for each row of the matrix. The height of each bar is the sum of the elements in the row.

y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
bar(y,'stacked')

Create a bar graph with categorical values along the x-axis. By default, the categories display in alphabetical order.

c = categorical({'apples','pears','oranges'});
prices = [1.23 0.99 2.3];
bar(c,prices)

Create a figure with two subplots. In the upper subplot, plot a bar graph. In the lower subplot, plot a stacked bar graph of the same data.

y = [1 2 3; 4 5 6];
ax1 = subplot(2,1,1);
bar(ax1,y)

ax2 = subplot(2,1,2); 
bar(ax2,y,'stacked')

Create a bar graph using red bars.

y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
bar(y,'r')

Set the bar interior color and outline color using RGB triplets. Set the width of the bar outline.

y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
bar(y,'FaceColor',[0 .5 .5],'EdgeColor',[0 .9 .9],'LineWidth',1.5)

Control individual bar colors using the CData property of the Bar object.

Create a bar chart and assign the Bar object to a variable. Set the FaceColor property of the Bar object to 'flat' so that the chart uses the colors defined in the CData property. By default, the CData property is prepopulated with a matrix of the default RGB color values. To change a particular color, change the corresponding row in the matrix. For example, change the color of the second bar.

b = bar(rand(10,1));
b.FaceColor = 'flat';
b.CData(2,:) = [.5 0 .5];

Create a bar chart that uses colormap colors by setting the FaceColor property to 'flat'. Then set the CData property for each Bar object to an integer.

y = [1 3 5; 3 2 7; 3 4 2];
b = bar(y,'FaceColor','flat');
for k = 1:size(y,2)
    b(k).CData = k;
end

Create a bar graph with a three-column matrix input and return the three bar series objects. bar creates one bar series for each column in the matrix.

y = [2 4 6; 3 4 5];
b = bar(y);

Change properties for a specific bar series by indexing into the object array. For example, change properties of the bars representing the second column of y using b(2). Starting in R2014b, you can use dot notation to set properties. If you are using an earlier release, use the set function instead.

b(2).LineWidth = 2;
b(2).EdgeColor = 'red';

Input Arguments

collapse all

x values, specified as a vector or a matrix. If x and y are both vectors, then they must be equal length. If x and y are both matrices, then they must be equal size. If x is a vector and y is a matrix, then the length of x must equal the number of rows in y.

The x values do not have to be in order, but they cannot contain duplicate values. If x is a matrix, then it cannot contain duplicate values across columns.

Example: 1:10

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | categorical | datetime | duration

y values, specified as a vector or a matrix.

  • If y is a vector, then bar draws one bar for each element. The bar function treats all vectors as column vectors.

  • If y is a matrix, then bar groups the bars according to the rows in y.

Example: [10 8 5 7 3 9 1]

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | duration

Bar width, specified as a fraction of the total space available for each bar. The default of 0.8 means the bar width is 80% of the space from the previous bar to the next bar, with 10% of that space on each side.

If the width is 1, then the bars within a group touch one another.

Example: 0.5

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Bar group style, specified by one of these values.

StylePurpose

'grouped'

Display one group for each row in y.

  • If y is an m-by-n matrix, then bar displays m groups of n vertical bars, where m is the number of rows and n is the number of columns in y.

  • If y is a vector of length n, then bar displays one group of n bars. The bar function treats all vectors as column vectors.

'stacked'

Display one bar for each row in y.

  • If y is an m-by-n matrix, then bar displays m bars where each bar height is the sum of the elements in the row. Each bar is multicolored. Colors correspond to distinct elements and show the relative contribution each row element makes to the total sum.

  • If y is a vector of length n, then bar displays n bars. The bar function treats all vectors as column vectors.

'histc'

Display the graph in histogram format, in which bars touch one another.

'hist'

Display the graph in histogram format, but center each bar over the x-ticks, rather than making bars span x-ticks as the histc option does.

Bar color, specified as one of the colors in this table.

ColorColor
'b'Blue
'r'Red
'g'Green
'c'Cyan
'm'Magenta
'y'Yellow
'k'Black
'w'White

Axes object. If you do not specify an axes, then bar uses the current axes for the bar graph.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: 'EdgeColor','g' specifies a green outline around the bars.

The Bar properties listed here are only a subset. For a complete list, see Bar Properties.

Note

You cannot specify Name,Value pairs when using the 'hist' or 'histc' bar group style options.

collapse all

Outline color, specified as an RGB triplet, 'flat', or one of the color options listed in the table. If there are 150 bars or fewer, the default value is [0 0 0], which corresponds to black. If there are more than 150 bars, the default value is 'none'.

Starting in R2017b, the 'flat' option uses the CData values to color the edges. In previous releases, the 'flat' option colored the edges using colors from the colormap.

For a custom color, specify an RGB triplet. An RGB triplet is a three-element row vector whose elements specify the intensities of the red, green, and blue components of the color. The intensities must be in the range [0,1]; for example, [0.4 0.6 0.7]. Alternatively, you can specify some common colors by name. This table lists the long and short color name options and the equivalent RGB triplet values.

OptionDescriptionEquivalent RGB Triplet
'red' or 'r'Red[1 0 0]
'green' or 'g'Green[0 1 0]
'blue' or 'b'Blue[0 0 1]
'yellow' or 'y'Yellow[1 1 0]
'magenta' or 'm'Magenta[1 0 1]
'cyan' or 'c'Cyan[0 1 1]
'white' or 'w'White[1 1 1]
'black' or 'k'Black[0 0 0]
'none'No colorNot applicable

Example: b = bar(1:10,'EdgeColor','red')

Example: b.EdgeColor = [0 0.5 0.5];

Example: b.EdgeColor = 'flat';

Fill color, specified as an RGB triplet, 'flat', or one of the color options listed in the table. The 'flat' option uses the CData values to color the faces.

For a custom color, specify an RGB triplet. An RGB triplet is a three-element row vector whose elements specify the intensities of the red, green, and blue components of the color. The intensities must be in the range [0,1]; for example, [0.4 0.6 0.7]. Alternatively, you can specify some common colors by name. This table lists the long and short color name options and the equivalent RGB triplet values.

OptionDescriptionEquivalent RGB Triplet
'red' or 'r'Red[1 0 0]
'green' or 'g'Green[0 1 0]
'blue' or 'b'Blue[0 0 1]
'yellow' or 'y'Yellow[1 1 0]
'magenta' or 'm'Magenta[1 0 1]
'cyan' or 'c'Cyan[0 1 1]
'white' or 'w'White[1 1 1]
'black' or 'k'Black[0 0 0]
'none'No colorNot applicable

Starting in R2017b, the default value is an RGB triplet from the ColorOrder property of the axes. In previous releases, the default value was 'flat' and the colors were based on the colormap.

Example: b = bar(1:10,'FaceColor','red')

Example: b.FaceColor = [0 0.5 0.5];

Example: b.FaceColor = 'flat';

Color data, specified as one of these values:

  • RGB triplet — Single RGB color value applies to all bars.

  • Three-column matrix — One color per bar. Each row in the matrix specifies an RGB triplet for a particular bar.

  • Scalar — Single color applies to all bars, where the color comes from the colormap.

  • Vector — One color per bar. The colors come from the colormap.

By default, when you create a bar chart, the CData property contains a three-column matrix of RGB triplets. You can change the color for a particular bar by changing the corresponding row in the matrix.

This property applies only when the FaceColor or EdgeColor property is set to 'flat'.

Example

Change the color for a particular bar by setting the FaceColor property to 'flat'. Then change the corresponding row in the CData matrix to the new RGB triplet. For example, change the color of the second bar.

b = bar(1:10,'FaceColor','flat');
b.CData(2,:) = [0 0.8 0.8];

Baseline value, specified as a numeric scalar value.

The baseline value that you specify applies to either the x-axis or the y-axis depending on the bar chart orientation. If you change the orientation of the bar chart from vertical to horizontal, or vice versa, the baseline value might change. Set the BaseValue property after setting the Horizontal property.

Line style of bar outlines, specified as one of the line styles in this table.

Line StyleLine StyleResulting Line
'-'Solid line

'--'Dashed line

':'Dotted line

'-.'Dash-dotted line

'none'No lineNo line

Width of bar outlines, specified as a positive value in point units. One point equals 1/72 inch.

Example: 1.5

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Output Arguments

collapse all

Bar objects. Use the elements in b to access and modify properties of a specific Bar object after it has been created.

Introduced before R2006a

Was this topic helpful?