Histogram (with sum of variables on y-axis)

12 visualizaciones (últimos 30 días)
Vlatko Milic
Vlatko Milic el 19 de Dic. de 2022
Comentada: Vlatko Milic el 20 de Dic. de 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.
Thanks in advance.

Respuesta aceptada

Adam Danz
Adam Danz el 19 de Dic. de 2022
Editada: Adam Danz el 19 de Dic. de 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')
  3 comentarios
Adam Danz
Adam Danz el 19 de Dic. de 2022
Glad it worked out! In case you're still curious, discretize is used in my example to bin your data. Take this example: I have a vector of random numbers between [1,10], and I want to bin them in two groups: values 1-5 (group 1) and values 6-10 (group 2):
x = randi(10,1,12)
x = 1×12
10 1 5 10 2 4 9 9 7 10 5 1
bins = [1 6 10]
bins = 1×3
1 6 10
discretize(x, bins)
ans = 1×12
2 1 1 2 1 1 2 2 2 2 1 1
Vlatko Milic
Vlatko Milic el 20 de Dic. de 2022
Now I understand the procedure, very pedagogically explained. Thanks and happy holidays!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Distribution Plots en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by