Adjust count values in histogram

10 views (last 30 days)
Sebastian
Sebastian on 1 Mar 2022
Commented: Adam Danz on 2 Mar 2022
Hello
I have a vector of temperature values taken in 1 second increments. I want to use the "histogram" command to show in which time period which temperatures were measured. This will work correctly for the time unit seconds. But I want to display these seconds (y-values) now in hours. I don't know how to change the "Values" attribute to do this.
Thanks for the help.
  8 Comments
jessupj
jessupj on 1 Mar 2022
I think you'd want to do something like this
ggg = gca;
ggg.YTickLabels = cellfun(@str2num, ggg.YTickLabels )/3600
that converts the current yticklabel strings to numbers and scales them by some factor. of course, this will be ugly without specificying precision.
Adam Danz
Adam Danz on 2 Mar 2022
That's clearer @Sebastian. If my answer doesn't address your question please let us know.

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 2 Mar 2022
If temperature data is collected a 1-second interval, then a histogram of temperature data will show the number of temperature samples within each bin which can also be interpreted as the amount of time in seconds that data was collected within each bin.
To convert time to hours, use histcounts to retreive the bin heights, divide by 60 twice to convert from seconds to hours, and then plot the results using histogram.
% Create demo data
x = randn(1,30000)*10 + 35;
% Compute bin counts at 10-deg bin widths (you can change this)
[counts, bins] = histcounts(x,'BinWidth', 10);
% Convert counts (one sample per second) to hours
countHrs = counts/60/60;
% plot results
histogram('BinEdges',bins,'BinCounts',countHrs)
ylabel('Sum of sample intervals (hrs)')
xlabel('Temperature (c)')

More Answers (1)

David Hill
David Hill on 1 Mar 2022
Use normalization (probability) to get what you want.
temp = randsample(1:1:100, 1e4, true, [linspace(eps, 1-eps, 50), flip(linspace(eps, 1-eps, 50))]);
histogram(temp,'Normalization','probability')
xlabel("Temperature")

Community Treasure Hunt

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

Start Hunting!