bary = [75 91 105 123.5 131 150 179 203 226 249 281.5]; bar(y) fig2plotly()
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) fig2plotly()
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) fig2plotly()
Display four groups of three bars.
y = [2 2 3; 2 5 6; 2 8 9; 2 11 12]; bar(y) fig2plotly()
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') fig2plotly()
Define
xas a vector of three year values. Defineyas a matrix that contains a combination of negative and positive values. Display the values in a bar graph.
x = [1980 1990 2000]; y = [15 20 -5; 10 -17 21; -10 5 15]; bar(x,y,'stacked') fig2plotly()
One way to indicate categories for your bars is to specify
Xas a categorical array. Thebarfunction uses a sorted list of the categories, so the bars might display in a different order than you expect. To preserve the order, call thereordercatsfunction.Define
Xas categorical array, and call thereordercatsfunction to specify the order for the bars. Then defineYas a vector of bar heights and display the bar graph.
X = categorical({'Small','Medium','Large','Extra Large'});
X = reordercats(X,{'Small','Medium','Large','Extra Large'});
Y = [10 21 33 52];
bar(X,Y)
fig2plotly()
Define
valsas a matrix containing the values of two data sets. Display the values in a bar graph and specify an output argument. Since there are two data sets,barreturns a vector containing twoBarobjects.
x = [1 2 3]; vals = [2 3 6; 11 23 26]; b = bar(x,vals); fig2plotly()
Display the values at the tips of the first series of bars. Get the coordinates of the tips of the bars by getting the
XEndPointsandYEndPointsproperties of the firstBarobject. Pass those coordinates to thetextfunction, and specify the vertical and horizontal alignment so that the values are centered above the tips of the bars.
xtips1 = b(1).XEndPoints;
ytips1 = b(1).YEndPoints;
labels1 = string(b(1).YData);
text(xtips1,ytips1,labels1,'HorizontalAlignment','center',...
'VerticalAlignment','bottom')
fig2plotly()
Next, display the values above the tips of the second series of bars.
xtips2 = b(2).XEndPoints;
ytips2 = b(2).YEndPoints;
labels2 = string(b(2).YData);
text(xtips2,ytips2,labels2,'HorizontalAlignment','center',...
'VerticalAlignment','bottom')
fig2plotly()
Starting in R2019b, you can display a tiling of bar graphs using the
tiledlayoutandnexttilefunctions. Call thetiledlayoutfunction to create a 2-by-1 tiled chart layout. Call thenexttilefunction to create the axes objectsax1andax2. Display a bar graph in the top axes. In the bottom axes, display a stacked bar graph of the same data.
y = [1 2 3; 4 5 6]; tiledlayout(2,1) % Top bar graph ax1 = nexttile; bar(ax1,y) % Bottom bar graph ax2 = nexttile; bar(ax2,y,'stacked') fig2plotly()
Create a bar graph using red bars.
y = [75 91 105 123.5 131 150 179 203 226 249 281.5]; bar(y,'r') fig2plotly()
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) fig2plotly()
Control individual bar colors using the
CDataproperty of theBarobject.Create a bar chart and assign the
Barobject to a variable. Set theFaceColorproperty of theBarobject to'flat'so that the chart uses the colors defined in theCDataproperty. By default, theCDataproperty 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]; fig2plotly()
Create a bar chart that uses colormap colors by setting the
FaceColorproperty to'flat'. Then set theCDataproperty for eachBarobject 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
fig2plotly()
Create matrix
y, where each column is a series of data. Call thebarfunction to display the data in a bar graph, and specify an output argument. The output is a vector of threeBarobjects, where each object corresponds to a different series. This is true whether the bars are grouped or stacked.
y = [10 15 20; 30 35 40; 50 55 62]; b = bar(y); fig2plotly()
Make the third series of bars green.
b(3).FaceColor = [.2 .6 .5]; fig2plotly()