Show Sidebar Hide Sidebar

Bar Charts in MATLAB

How to make a bar chart in MATLAB. Seven examples of grouped, stacked, overlaid, and colored bar charts.

% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

x = 1900:10:2000;
y = [75.995,91.972,105.711,123.203,131.669,...
     150.697,179.323,203.212,226.505,249.633,281.422];

fig = figure;
bar(x,y);

%--PLOTLY--%

% Strip MATLAB style by default!
response = fig2plotly(fig, 'filename', 'matlab-basic-bar');
plotly_url = response.url;
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

Y = [1.0 0.5 0.7
     2.0 1.5 2.0
     5.0 4.0 5.0
     4.0 4.0 4.5
     3.0 2.0 2.0];

fig = figure;
bar(Y,'group');

% PLOTLY
response = fig2plotly(fig, 'filename', 'matlab-grouped-bar', 'strip', false);
plotly_url = response.url;
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

% Create data for childhood disease cases
measles = [38556 24472 14556 18060 19549 8122 28541 7880 3283 4135 7953 1884]';
mumps = [20178 23536 34561 37395 36072 32237 18597 9408 6005 6268 8963 13882]';
chickenPox = [37140 32169 37533 39103 33244 23269 16737 5411 3435 6052 12825 23332]';

% Create a stacked bar chart using the bar function
fig = figure;
bar(1:12, [measles mumps chickenPox], 0.5, 'stack');
axis([0 13 0 100000]);
title('Childhood diseases by month');
xlabel('Month');
ylabel('Cases (in thousands)');
legend('Measles', 'Mumps', 'Chicken pox');

%--PLOTLY--%

% Strip MATLAB style by default!
response = fig2plotly(fig, 'filename', 'matlab-stacked-bar');
plotly_url = response.url;
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api
y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
bar(y,'stacked');
title('Bar Graph with Legend');
legend('show');

response = fig2plotly;
plotly_url = response.url;
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

%bins
x = 1900:10:2000;

%data
y1 = [75.99,91.92,105.71,...
       123.23,131.69,...
       150.67,179.33,203.12,...
       226.55,249.63,281.42];
%data
y2 = [55.2,61.972,65.71,...
       76.23,87.669,...
       91.7,103.23,124.21,...
       130.55,135.63,145.22];

%create MATLAB bar chart
fig = figure;
bar(x, y1);
hold on
bar(x, y2,'r');

%add legend
legend('group1','group2');

%--PLOTLY--%

% Strip MATLAB style by default!
response = fig2plotly(fig, 'filename', 'matlab-overlaid-bar';
plotly_url = response.url;
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

%bins
x = 1900:10:2000;

%data
y1 = [75.99,91.92,105.71,...
       123.23,131.69,...
       150.67,179.33,203.12,...
       226.55,249.63,281.42];
%data
y2 = [55.2,61.972,65.71,...
       76.23,87.669,...
       91.7,103.23,124.21,...
       130.55,135.63,145.22];

%create MATLAB bar chart
fig = figure('Color','w');
bar(x, [y1' y2'],'grouped');
ax = get(gca);
cat = ax.Children;

%set the first bar chart style
set(cat(2),'FaceColor',[145 25 206]/255,'BarWidth',2);

%set the second bar chart style
set(cat(1),'FaceColor',[45 125 206]/255,'BarWidth',2);

%set the axes style
set(gca,'box','off');

%--PLOTLY--%

% Strip MATLAB style by default!
response = fig2plotly(fig, 'filename', 'matlab-style-bar');
plotly_url = response.url;
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

x = [1,2,3];
y1 = [100 120 140];
y2 = [120 140 160];

trace1 = struct('x',x, 'y',y1, 'type','bar');
trace2 = struct('x',x, 'y',y2, 'type','bar');
data = {trace1 trace2};
layout = struct('title','Bar Graph with Custom Width', 'bargap',0.4);
response = plotly(data, struct('layout',layout));
plotly_url = response.url;
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

trace1 = struct(...
  'x', { {'giraffes', 'orangutans', 'monkeys'} }, ...
  'y', [20, 14, 23], ...
  'name', 'SF Zoo', ...
  'type', 'bar');
trace2 = struct(...
  'x', { {'giraffes', 'orangutans', 'monkeys'} }, ...
  'y', [12, 18, 29], ...
  'name', 'LA Zoo', ...
  'type', 'bar');
data = {trace1, trace2};
layout = struct('barmode', 'group');
response = plotly(data, struct('layout', layout, 'filename', 'grouped-bar', 'fileopt', 'overwrite'));
plot_url = response.url
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

trace1 = struct(...
  'x', { {'giraffes', 'orangutans', 'monkeys'} }, ...
  'y', [20, 14, 23], ...
  'name', 'SF Zoo', ...
  'type', 'bar');
trace2 = struct(...
  'x', { {'giraffes', 'orangutans', 'monkeys'} }, ...
  'y', [12, 18, 29], ...
  'name', 'LA Zoo', ...
  'type', 'bar');
data = {trace1, trace2};
layout = struct('barmode', 'stack');
response = plotly(data, struct('layout', layout, 'filename', 'stacked-bar', 'fileopt', 'overwrite'));
plot_url = response.url
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

trace1 = struct(...
  'x', [1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012], ...
  'y', [219, 146, 112, 127, 124, 180, 236, 207, 236, 263, 350, 430, 474, 526, 488, 537, 500, 439], ...
  'name', 'Rest of world', ...
  'marker', struct('color', 'rgb(55, 83, 109)'), ...
  'type', 'bar');
trace2 = struct(...
  'x', [1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012], ...
  'y', [16, 13, 10, 11, 28, 37, 43, 55, 56, 88, 105, 156, 270, 299, 340, 403, 549, 499], ...
  'name', 'China', ...
  'marker', struct('color', 'rgb(26, 118, 255)'), ...
  'type', 'bar');
data = {trace1, trace2};
layout = struct(...
    'title', 'US Export of Plastic Scrap', ...
    'xaxis', struct('tickfont', struct(...
        'size', 14, ...
        'color', 'rgb(107, 107, 107)')), ...
    'yaxis', struct(...
      'title', 'USD (millions)', ...
      'titlefont', struct(...
        'size', 16, ...
        'color', 'rgb(107, 107, 107)'), ...
      'tickfont', struct(...
        'size', 14, ...
        'color', 'rgb(107, 107, 107)')), ...
    'legend', struct(...
      'x', 0, ...
      'y', 1.0, ...
      'bgcolor', 'rgba(255, 255, 255, 0)', ...
      'bordercolor', 'rgba(255, 255, 255, 0)'), ...
    'barmode', 'group', ...
    'bargap', 0.15, ...
    'bargroupgap', 0.1);
response = plotly(data, struct('layout', layout, 'filename', 'style-bar', 'fileopt', 'overwrite'));
plot_url = response.url
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

data = {...
  struct(...
    'x', { {'Liam', 'Sophie', 'Jacob', 'Mia', 'William', 'Olivia'} }, ...
    'y', [8.0, 8.0, 12.0, 12.0, 13.0, 20.0], ...
    'text', { {'4.17 below the mean', '4.17 below the mean', '0.17 below the mean', '0.17 below the mean', '0.83 above the mean', '7.83 above the mean'} }, ...
    'marker', struct('color', 'rgb(142, 124, 195)'), ...
    'type', 'bar')...
};
layout = struct(...
    'title', 'Number of graphs made this week', ...
    'font', struct('family', 'Raleway, sans-serif'), ...
    'showlegend', false, ...
    'xaxis', struct('tickangle', -45), ...
    'yaxis', struct(...
      'zeroline', false, ...
      'gridwidth', 2), ...
    'bargap', 0.05);
response = plotly(data, struct('layout', layout, 'filename', 'bar-with-hover-text', 'fileopt', 'overwrite'));
plot_url = response.url
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

data = {...
  struct(...
    'x', [1, 2, 3, 4], ...
    'y', [5, 4, -3, 2], ...
    'marker', struct('color', { {'#447adb', '#447adb', '#db5a44', '#447adb'} }), ...
    'type', 'bar')...
};
response = plotly(data, struct('filename', 'bar-marker-array', 'fileopt', 'overwrite'));
plot_url = response.url
Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.