Adjusting Y-Values in Histograms

Hello,
This might sound like a simple question, but how do I change the y-values of the histograms in the figure below (bottom panels)? Both the left and right histograms have bins with only 2 or 3 y-values, meaning they are binned. How can I adjust this?
The code is simply:
histogram(-xuncL(:,2), 'BinWidth', BWidth, 'Normalization', 'Probability', 'Facecolor', 'c')
Thank you!

3 comentarios

Umar
Umar el 11 de Jul. de 2024
Hi Alberto,
By using 'histcounts' to compute bin counts and then customizing the counts before plotting the histogram, you can precisely control the y-values for each bin in your histogram visualization. For more information on histcounts function, please refer to https://www.mathworks.com/help/matlab/ref/histcounts.html
I will further illustrate it with example below.
% Calculate bin counts
[counts, edges] = histcounts(-xuncL(:,2), 'BinWidth', BWidth);
% Adjust y-values as needed
counts(counts == 2) = {desired_value}; % Replace 'desired_value' with the value you want counts(counts == 3) = {another_value}; % Replace 'another_value' with the value you prefer
% Plot histogram with custom y-values
bar(edges(1:end-1), counts, 'hist');
Hope this will help resolve your problem.
alberto tonizzo
alberto tonizzo el 12 de Jul. de 2024
Thank you!
Umar
Umar el 12 de Jul. de 2024
No problem Alberto, glad to help out. If you have any further questions, please let us know.

Iniciar sesión para comentar.

Respuestas (2)

the cyclist
the cyclist el 11 de Jul. de 2024
Your question is not perfectly clear to me. If you don't want the values binned, I think you more likely want to use the bar function rather than the histogram function to plot them. By construction, histograms are for binning!
Plot bars with x positions where the data are, y values of one.
N = 5;
x = rand(N,1); % <-- Made up data
y = ones(N,1);
bar(x,y)

1 comentario

Umar
Umar el 11 de Jul. de 2024
Hi @cyclist,
I do appreciate your contribution and helping out to resolve this problem.

Iniciar sesión para comentar.

DGM
DGM el 12 de Jul. de 2024
Editada: DGM el 12 de Jul. de 2024
The reason that the bar heights have discrete values is simply that the height represents an underlying integer count, and you have relatively few things to count compared to the number of bins (along x) that you have. Consequently, you have only a few counts in each bin. Based on your images, I'm estimating that there are only about 42 items being counted in total.
A = randi([1 100],50,1);
histogram(A, 'BinWidth', 1, 'Normalization', 'Probability', 'Facecolor', 'c')
At least that's my suspicion.

Productos

Versión

R2022a

Etiquetas

Preguntada:

el 11 de Jul. de 2024

Comentada:

el 12 de Jul. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by