# Histogram (with sum of variables on y-axis)

25 views (last 30 days)
Vlatko Milic on 19 Dec 2022
Commented: Vlatko Milic on 20 Dec 2022
Hi,
I have two variables consisting of 20 values each. I want to create a histogram where I sum the values in an interval on the x-axis, such as all values between 0 to 5, and plot the associated sum on the y-axis. Hence, I am not interested in the count on the y-axis, but the sum of the variables within that range.

Adam Danz on 19 Dec 2022
Edited: Adam Danz on 19 Dec 2022
Hello Vlatko,
I'm not sure if you want to sum the two vectors and then compute the binned sum or if you want to compute the binned sum on each vector independently. My demo below does the latter, it computes the binned sum on each vector and plots both results. It should be fairly straightforward to sum the data first if that's what you're aiming for.
1. I create demo data, you can skip this part and replace the variable names (v1, v2) with your data.
2. Using discretize, I bin the two vectors according to thier values in steps of 5
3. Using accumarray, I sum the vectors within each bin. Another possibility is using groupsummary but one benefit of accumarray is that it will return 0s for missing bins.
4. Lastly, plot the result using histogram while specifying the bins and bin counts rather than letting the histogram function do the count calculations.
% Create demo data: two 1x20 vectors of values 0:50
rng default
v1 = 50*rand(1,20);
v2 = 50*rand(1,20);
Assuming your variables are v1 sand v2, the rest should be ready to go with your data.
% Assign each value to a bin
bins = 0:5:50; % Assuming the values are within the range [0,50]
bin1 = discretize(v1,bins);
bin2 = discretize(v2,bins);
% Compute sum of data within each bin
v1sum = accumarray(bin1(:),v1(:));
v2sum = accumarray(bin2(:),v2(:));
% Plot histogram of sums
figure()
histogram('BinEdges',bins,'BinCounts',v1sum,'DisplayName','V1')
hold on
histogram('BinEdges',bins,'BinCounts',v2sum,'DisplayName','v2')
ylabel('bin sum')
xlabel('bin')
legend('location','bestoutside') Vlatko Milic on 20 Dec 2022
Now I understand the procedure, very pedagogically explained. Thanks and happy holidays!

### Categories

Find more on Histograms in Help Center and File Exchange

R2021b

### Community Treasure Hunt

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

Start Hunting!