Challenge - 5 Problems
Bar and Histogram Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Bar Plot with Custom Colors
What will be the color of the bars in the following MATLAB code?
MATLAB
y = [5 10 15]; bar(y, 'FaceColor', [0 0.5 0]);
Attempts:
2 left
💡 Hint
FaceColor property sets the bar color using RGB values between 0 and 1.
✗ Incorrect
The FaceColor property is set to [0 0.5 0], which means no red, half green, no blue, resulting in a medium green color.
❓ Predict Output
intermediate2:00remaining
Histogram Bin Count Output
What is the output of the following MATLAB code?
MATLAB
data = [1 2 2 3 4 4 4 5]; h = histogram(data, 4); disp(h.BinCounts);
Attempts:
2 left
💡 Hint
Bins are automatically spaced to cover the data range with 4 bins.
✗ Incorrect
The data range is from 1 to 5. With 4 bins, the bins cover intervals [1-2), [2-3), [3-4), [4-5]. Counting values in each bin gives [1 2 1 4].
🔧 Debug
advanced2:00remaining
Fix the Error in Bar Plot Code
The following MATLAB code produces an error. Which option fixes it so the bar plot shows correctly?
MATLAB
x = [1 2 3]; y = [4 5]; bar(x, y);
Attempts:
2 left
💡 Hint
x and y vectors must have the same length for bar(x,y).
✗ Incorrect
The error occurs because x and y have different lengths. Making y length 3 matches x and fixes the error.
📝 Syntax
advanced2:00remaining
Identify Syntax Error in Histogram Code
Which option contains a syntax error in MATLAB histogram plotting?
Attempts:
2 left
💡 Hint
MATLAB uses name-value pairs with quotes, not equal signs.
✗ Incorrect
Option D uses an equal sign (=) which is invalid syntax for name-value pairs in MATLAB. Correct syntax uses comma and quotes.
🚀 Application
expert3:00remaining
Number of Bars in Grouped Bar Plot
Given the following MATLAB code, how many bars will be displayed in the plot?
MATLAB
data = [3 5 2; 4 6 1]; bar(data, 'grouped');
Attempts:
2 left
💡 Hint
Each row is a group, each column is a bar within the group.
✗ Incorrect
The data matrix has 2 rows and 3 columns. Grouped bar plot shows 3 bars per group, 2 groups total, so 6 bars.