Bar and histogram plots help us see data clearly by showing counts or values as bars. They make it easy to compare groups or understand data distribution.
To compare sales numbers for different products in a store.
To show how many students scored in different grade ranges.
To visualize the frequency of different ages in a group.
To display monthly expenses by category.
To understand the spread of test scores in a class.
Syntax
MATLAB
bar(X, Y)
histogram(data)
% X and Y are vectors for bar plot
% data is a vector for histogram
bar(X, Y) creates a bar plot where X is categories and Y is values.
histogram(data) creates a histogram showing data distribution.
Examples
Simple bar plot with categories 1 to 4 and their values.
MATLAB
x = [1234];
y = [1020155];
bar(x, y);
Histogram of 100 random numbers from a normal distribution.
This code first shows a bar plot of fruit sales with category names. Then it creates a histogram of 100 random integers from 1 to 10.
MATLAB
% Create sample data
categories = {'Apples', 'Bananas', 'Cherries'};
sales = [304525];
% Bar plot of sales
bar(sales);
set(gca, 'XTickLabel', categories);
title('Fruit Sales');
ylabel('Number Sold');
% Create random data for histogram
data = randi([110], 100, 1);
% New figure for histogram
figure;
histogram(data);
title('Random Data Distribution');
xlabel('Value');
ylabel('Frequency');
OutputSuccess
Important Notes
Use set(gca, 'XTickLabel', categories) to label bars with names.
Histograms automatically group data into bins to show frequency.
Use figure; to create a new plot window if you want multiple plots.
Summary
Bar plots show values for categories as bars.
Histograms show how data is spread across ranges.
Both help visualize data clearly and quickly.
Practice
(1/5)
1. Which of the following best describes the difference between a bar plot and a histogram in MATLAB?
easy
A. Both bar plots and histograms display values for specific categories.
B. Bar plots show data distribution over ranges; histograms display values for specific categories.
C. Both bar plots and histograms show data distribution over ranges.
D. Bar plots display values for specific categories; histograms show data distribution over ranges.
Solution
Step 1: Understand bar plot purpose
Bar plots show values for distinct categories or positions, like sales per month.
Step 2: Understand histogram purpose
Histograms group data into ranges (bins) to show how data spreads, like exam scores distribution.
Final Answer:
Bar plots display values for specific categories; histograms show data distribution over ranges. -> Option D
Quick Check:
Bar = categories, Histogram = ranges [OK]
Hint: Bar = categories, Histogram = data spread [OK]
Common Mistakes:
Confusing bar plots with histograms
Thinking both show the same type of data
Assuming histograms show exact values per category
2. Which MATLAB command correctly creates a bar plot for the vector y = [3, 7, 5]?
easy
A. bar(y)
B. histogram(y)
C. plotbar(y)
D. barplot(y)
Solution
Step 1: Identify correct function for bar plot
The MATLAB function to create a bar plot is bar().
Step 2: Check syntax correctness
Calling bar(y) plots the values in vector y as bars.
Final Answer:
bar(y) -> Option A
Quick Check:
Use bar() for bar plots [OK]
Hint: Use bar() to plot bar charts, histogram() for histograms [OK]
Common Mistakes:
Using histogram() instead of bar()
Using non-existent functions like plotbar()
Incorrect function names or typos
3. What will be the output of the following MATLAB code?
data = [1, 2, 2, 3, 3, 3, 4];
histogram(data, 3)
medium
A. An error because histogram requires vector of edges
B. A histogram with 3 bins showing counts of data in ranges covering 1 to 4
C. A bar plot with bars at positions 1, 2, 3, 4
D. A line plot connecting the data points
Solution
Step 1: Understand histogram function with bin count
Calling histogram(data, 3) creates a histogram with 3 bins dividing the data range.
Step 2: Analyze data distribution
Data ranges from 1 to 4, so bins cover intervals like [1-2), [2-3), [3-4]. Counts show how many values fall in each bin.
Final Answer:
A histogram with 3 bins showing counts of data in ranges covering 1 to 4 -> Option B
Quick Check:
histogram(data, bins) = bins count [OK]
Hint: histogram(data, n) makes n bins for data spread [OK]
Common Mistakes:
Confusing histogram with bar plot output
Expecting exact bars at data points
Thinking histogram needs edges vector always
4. The following MATLAB code is intended to create a bar plot of y = [4, 6, 8], but it produces an error. What is the error?
bar y
medium
A. Incorrect function name; should be histogram
B. Vector y is not defined
C. Missing parentheses around the vector argument
D. bar function cannot plot vectors
Solution
Step 1: Check syntax of bar function call
The correct syntax requires parentheses: bar(y).
Step 2: Identify cause of error
Writing bar y without parentheses causes MATLAB syntax error.
Final Answer:
Missing parentheses around the vector argument -> Option C
Quick Check:
Function calls need parentheses [OK]
Hint: Always use parentheses when calling functions [OK]
Common Mistakes:
Omitting parentheses in function calls
Confusing bar with histogram function
Assuming vector y is undefined without checking
5. You have a dataset of exam scores: scores = [55, 70, 70, 85, 90, 90, 90, 100]. You want to create a histogram with bins of width 10 (like 50-59, 60-69, etc.). Which MATLAB command correctly creates this histogram?
hard
A. histogram(scores, 'BinWidth', 10)
B. bar(scores, 10)
C. histogram(scores, 10)
D. bar(scores, 'BinWidth', 10)
Solution
Step 1: Understand how to set bin width in histogram
Use histogram(data, 'BinWidth', value) to specify bin size.
Step 2: Check options for correct syntax
histogram(scores, 'BinWidth', 10) uses correct syntax to set bin width to 10 for scores.
Step 3: Identify incorrect options
Options B and D use bar function incorrectly; C sets number of bins, not width.
Final Answer:
histogram(scores, 'BinWidth', 10) -> Option A
Quick Check:
Use 'BinWidth' to set histogram bin size [OK]
Hint: Use 'BinWidth' property to set histogram bin size [OK]
Common Mistakes:
Using bar instead of histogram for bin width
Passing number instead of 'BinWidth' name-value pair
Confusing bin count with bin width
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.