My Plot function works but the graphics window that pops up is blank.

I am trying to plot a Histogram of Age for a class in a script and I am unable to see the actual plot or axes or anything on the graph. I am given a graphics screen with a ribbon on the top with all of the tools, but there is nothing else. Is this a graphics issue or is there a function I am missing? Here is the Script:
Age=rand([20,1])*20+20
[counts,centers]=hist(Age, [22.5:5:37.5]);
centers'
counts'
xlabel('Age (in Years)')
ylabel('Freq. of Records')
title('Histogram of Age')
I have no idea what is wrong.

Answers (1)

When you call the hist command and ask for output, it does not plot anything.
Age=rand([20,1])*20+20;
hist(Age, [22.5:5:37.5]); % Remove the output from this line
xlabel('Age (in Years)')
ylabel('Freq. of Records')
title('Histogram of Age')
If you want the output and also the plot, you have two options:
1) Call hist twice (once with output, and again without output):
Age=rand([20,1])*20+20;
hist(Age, [22.5:5:37.5]); % No output, this produces the plot
xlabel('Age (in Years)')
ylabel('Freq. of Records')
title('Histogram of Age')
[counts,centers]=hist(Age, [22.5:5:37.5]); % With output, no plot.
2) Call bar with the output:
Age=rand([20,1])*20+20;
[counts,centers]=hist(Age, [22.5:5:37.5]); % With output, no plot.
bar(centers, counts, 'hist') % Plot using bar instead.
xlabel('Age (in Years)')
ylabel('Freq. of Records')
title('Histogram of Age')
Note: There are two new functions, histcounts and histogram which are recommended over hist. In addition to eliminating the confusion about whether it creates a plot or not, histogram also creates much nicer looking charts.
Age=rand([20,1])*20+20;
histogram(Age, 20:5:40);
xlabel('Age (in Years)')
ylabel('Freq. of Records')
title('Histogram of Age')
Or:
Age=rand([20,1])*20+20;
[counts,edges] = histcounts(Age, 20:5:40);
histogram('BinEdges',edges,'BinCounts',counts)
xlabel('Age (in Years)')
ylabel('Freq. of Records')
title('Histogram of Age')

Asked:

on 31 Jan 2018

Edited:

on 1 Feb 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!