Adjust count values in histogram

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

On histogram you expect to see the distribution of data itself, so I'm not sure what you exactly mean by second or hr. Say you have a vector of temp for temperature measured at different time points (as you said, in 1 sec intervals):
temp = randsample(1:1:100, 1e4, true, [linspace(eps, 1-eps, 50), flip(linspace(eps, 1-eps, 50))]);
histogram(temp)
xlabel("Temperature")
Let's use your histogram as an example: I see from it that the temperature value 40°C was measured in a period of about 500 seconds. I just want to show in this plot that this temperature value was measured in a period of 0.14 hours. (500/3600 = 0.14)
Ive J
Ive J on 1 Mar 2022
Edited: Ive J on 1 Mar 2022
"I see from it that the temperature value 40°C was measured in a period of about 500 seconds"
No! it shows the measurement count within the bin. For instance the image below shows there are 482 temperature values within the range (bin) of [39, 42):
If you're interested to change y-axis, see histogram property Normalization: https://www.mathworks.com/help/matlab/ref/matlab.graphics.chart.primitive.histogram-properties.html
Sebastian
Sebastian on 1 Mar 2022
Edited: Sebastian on 1 Mar 2022
Yes, I know, but that's not the point. I only want to convert the value 482, which in my case represent 482 seconds, into the time specification hour. I want to divide the values by 3600 and have them displayed that way.
> Let's use your histogram as an example: I see from it that the temperature value 40°C was measured in a period of about 500 seconds.
As @Ive J mentioned, this is not what the histogram is showing. The y axis of histograms are typically counts, probabilities, frequencies, etc. The y-axis shows the density of values within each x-bin.
Perhaps you could elaborate on your goal and provide an example.
Sebastian
Sebastian on 1 Mar 2022
Edited: Sebastian on 1 Mar 2022
I am not concerned with the temperature range. It should remain the same. I only want to scale the number of measured temperatures in this range in hours.
My example:
A temperature value is recorded every second for 3600 seconds. Of these, 1440 values are in the range between 30-40°C. So my histogram shows me Value: 1440 (which means 1440 seconds) BinEdges: [30 40].
I just want the histogram to show the value in hours in the y-axis, so the value 1440 scaled to 0.4 (h).
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.
That's clearer @Sebastian. If my answer doesn't address your question please let us know.

Sign in to comment.

 Accepted Answer

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)

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")

Categories

Asked:

on 1 Mar 2022

Commented:

on 2 Mar 2022

Community Treasure Hunt

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

Start Hunting!