Using Histogram to create a figure with multiple data sets
Show older comments
Hello! I'm new to MATLAB and am learning how to plot data. I have data stored in multiple double arrays. I am trying to plot them all in a histogram, where each bin can store the values in that range from the multiple doubles. My issue that i am having is that the code below overlays multiple histograms over top of each other rather than group them into one histogram. I hope this makes sense. I appreciate any help.
What I have so far is this (after I have imported my data, i'll skip that section)
figure(1);
a = Initial(1).red;
b = Initial(1).green;
c = Initial(2).red;
d = Initial(2).green;
e = Initial(4).red;
f = Initial(4).green;
NumBins = 20;
histogram(a/b,NumBins,'FaceColor','black'); hold on
histogram(c/d,NumBins,'FaceColor','black');
histogram(e/f,NumBins,'FaceColor','black')
ylim([0 20]);
set(gca, 'XScale', 'log');
4 Comments
Image Analyst
on 30 Sep 2021
What is a, etc.? Scalars, Matrices, ????
What is a/b? If they're vectors or Matrices you might want to use a./b to do an element by element divide.
Not sure what you want. Do you want a single histogram (bar chart) with counts from a-f all grouped into a single set of bins?
Michelle Jackson
on 30 Sep 2021
Image Analyst
on 30 Sep 2021
Make sure you're using the right division. I think you are not. Run this:
format short g
m1 = rand(4, 9);
m2 = rand(4, 9);
div1 = m1 ./ m2 % Element by element divide
div2 = m1 / m2 % Matrix divide (multiply by invers matrix).
What do you observe?
You should be able to get all in one histogram if they are all the same size like
histogram([a(:), b(:), c(:), d(:), e(:), f(:)]);
Michelle Jackson
on 30 Sep 2021
Answers (1)
the cyclist
on 30 Sep 2021
0 votes
One possibility would be to use histcounts to get the bin counts (instead of histogram to plot the bins directly), and then a stacked or grouped bar chart for the plotting of those counts.
1 Comment
Michelle Jackson
on 30 Sep 2021
Categories
Find more on Data Distribution Plots 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!