TExcelTAD
Home › MATLAB / Octave › Bar, Histogram, Pie & Scatter

Bar, Histogram, Pie & Scatter

Line plots suit continuous data. For categories, distributions, proportions and relationships between two variables, MATLAB has dedicated chart functions. They follow the same pattern as plot and accept the same title, xlabel, legend and grid commands.

Chart Function Best for
Bar chart bar, barh Comparing values across categories
Histogram hist (both), histogram (MATLAB) The distribution of one variable
Pie chart pie Parts of a whole (few categories)
Scatter plot scatter The relationship between two variables
Others area, stairs, stem, errorbar, boxplot

Bar charts

bar(y) draws one bar per element. Label the categories with tick labels:

matlab
fruit = {'Apples', 'Pears', 'Plums', 'Cherries'};
sold = [42 27 15 33];
bar(sold)
set(gca, 'XTickLabel', fruit)
ylabel('Boxes sold')
title('Market stall, Saturday')
matlab · try it yourself
fruit = {'Apples', 'Pears', 'Plums', 'Cherries'};
sold = [42 27 15 33];
[~, order] = sort(sold, 'descend');
fprintf('Ranking: %s\n', strjoin(fruit(order), ' > '));
fprintf('Share of apples: %.1f%%\n', 100 * sold(1) / sum(sold));
Ranking: Apples > Cherries > Pears > Plums
Share of apples: 35.9%

set(gca, 'XTickLabel', ...) works in every version of MATLAB and Octave; newer MATLAB also has xticklabels(fruit) and accepts categorical data directly. With a matrix, bar(M) draws grouped bars (one group per row), and bar(M, 'stacked') stacks them. Use barh for horizontal bars.

Histograms

A histogram splits the range of the data into bins and shows how many values fall in each. hist(data, n) draws one with n bins; called with output arguments it returns the counts and bin centres without drawing anything, which is how the runnable part below gets the numbers:

matlab
data = [4.1 5.6 5.0 6.3 4.8 5.5 7.2 5.9 6.1 4.4 ...
        5.2 6.8 5.7 6.0 3.9 5.3 6.6 5.1 4.9 5.8];
hist(data, 5)
xlabel('Reaction time (s)'); ylabel('Count')
matlab · try it yourself
data = [4.1 5.6 5.0 6.3 4.8 5.5 7.2 5.9 6.1 4.4 ...
        5.2 6.8 5.7 6.0 3.9 5.3 6.6 5.1 4.9 5.8];
[counts, centers] = hist(data, 5);
fprintf('bin centre %.2f: %d values\n', [centers; counts]);
bin centre 4.23: 3 values
bin centre 4.89: 5 values
bin centre 5.55: 5 values
bin centre 6.21: 4 values
bin centre 6.87: 3 values

In MATLAB, the recommended modern function is histogram(data, 5), which draws the same chart and returns an object with properties like BinEdges and Values. Octave doesn't have histogram, so this course uses hist, which both programs support.

Pie charts

pie(x) divides a circle in proportion to the values. A second argument gives labels (or, as a vector of 0s and 1s, "explodes" slices out of the circle):

matlab
budget = [1200 450 300 250];
labels = {'Rent', 'Food', 'Transport', 'Other'};
pie(budget, labels)
title('Monthly budget')
matlab · try it yourself
budget = [1200 450 300 250];
labels = {'Rent', 'Food', 'Transport', 'Other'};
pct = 100 * budget / sum(budget);
for k = 1:numel(budget)
    fprintf('%-10s %5.1f%%\n', labels{k}, pct(k));
end
Rent        54.5%
Food        20.5%
Transport   13.6%
Other       11.4%

Pie charts are hard to read with more than about five slices or with similar values; a bar chart is usually clearer.

Scatter plots

scatter(x, y) draws a marker at each point without connecting them, which shows how two variables relate. Optional arguments set the marker size and colour, and 'filled' fills the markers:

matlab
height = [158 163 170 175 181 167 172 178 160 185];   % cm
weight = [52 60 66 72 80 61 70 75 55 84];             % kg
scatter(height, weight, 40, 'filled')
xlabel('Height (cm)'); ylabel('Weight (kg)')
title('Height vs weight')
matlab · try it yourself
height = [158 163 170 175 181 167 172 178 160 185];
weight = [52 60 66 72 80 61 70 75 55 84];
R = corrcoef(height, weight);
fprintf('Correlation coefficient: %.3f\n', R(1, 2));
Correlation coefficient: 0.995

A correlation close to 1 means the points lie almost on a rising straight line, which is what the scatter plot shows. plot(x, y, 'o') draws a similar picture, but scatter can also vary the size and colour of each marker individually.

Test yourself

Which chart shows how many values fall into each range of a single variable?

  1. Pie chart
  2. Histogram
  3. Scatter plot
  4. Line plot

What does [counts, centers] = hist(data, 5) do?

  1. Draws a histogram and returns nothing
  2. Returns the counts and bin centres for 5 bins without drawing
  3. Returns the 5 largest values
  4. Sorts the data into 5 groups

Which function best shows the relationship between height and weight measurements?

  1. pie
  2. bar
  3. scatter
  4. hist