How to make the y axis 'counts' and x axis the variable on Histograms
Show older comments
How to make the y axis 'counts' and x axis the variable on Histograms? The default is to have counts on the x axis and I want them on the y
3 Comments
> How to make the y axis 'counts' and x axis the variable on Histograms?
That's the standard format of histograms. The two existing answers below create a horizontal histogram which puts the counts on the x-axis and the variable values on the y-axis.
histogram(randn(1,1000))
xlabel('Variable name')
ylabel('Counts per bin')
Matt Egan
on 3 Dec 2020
Adam Danz
on 3 Dec 2020
The statement in your question, "The default is to have counts on the x axis and I want them on the y" is incorrect. The y-axis in histograms typically represents counts, frequencies, percentages, or other statistics about the data within each bin. The x-axis represents the bins for whatever variable you're plotting (weight, height, age, etc).
> all that happens is I have relabled my axis to represent the wrong thing
You'll have to explain your goal and your interpretation of the axes more clearly.
Answers (2)
Ameer Hamza
on 3 Dec 2020
Edited: Ameer Hamza
on 3 Dec 2020
One option is to just rotate the view
x = rand(1000,1);
histogram(x)
view(90, 90)
Combining histcounts() with barh() is another option.
Set the orientation property to horizontal.
histogram(rand(1,1000),'Orientation','horizontal')
Categories
Find more on Histograms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
