Why does histogram gives issues? I rephrased my question
Show older comments
I have the following set of data:
fitness1=rand(100,1)*1e-7;
fitness2=rand(100,1)*1e-5;
fitness3=rand(100,1)*1e-3;
Each set of fitness1, fitness2 and fitness3 has 100 independent elements. I want to plot its histogram such that the x-axis is logrithmic having values like 10^-2, 10^-3, 10^-4....and the y-axis shows us how many elements have that corresponding fitness values in each fitness? Say for example we have 10^-3 on axis, so how many values in each fitness1, fitness2 and fitness3 are there having values in the range of 10^-3. Likewise, if we have 10^-5 on x-axis, then how many values in side each fitness are there having this range of 10^-5 and so on. I tried the following but its not like the one in the attachment.
clear all
clc
load 2sn35
one=sort(one,'descend');
fitness2sn35=one;
load 2sn45
one=sort(one,'descend');
fitness2sn45=one;
[~,edges1] = histcounts(log10(fitness2sn35));%[~,edges] = histcounts(log10(x));
[~,edges2] = histcounts(log10(fitness2sn45));
histogram([log10(fitness2sn35) log10(fitness2sn45)] )% histogram(log10(fitness2sn35))
xticklabels(num2cell(10.^get(gca,'XTick')));
I want a grpah like this, but I don't get like this?
Answers (2)
x = rand(1, 1e5)*1e-5;
ax = axes;
Specify bins edges with logarithmic spacing.
h = histogram(ax, x, 10.^(-10:-5));
Set the XScale property of the axes to 'log'. You need to do this after the call to histogram not before.
ax.XScale = 'log';
8 Comments
Sadiq Akbar
on 17 Nov 2022
Steven Lord
on 17 Nov 2022
That's correct. I used simpler data to illustrate the technique. Now you can use that as a model for your code.
That preview looks like a smaller axes with its own histogram inside it. Just use this same technique with a smaller axes. But why do you need a preview that's basically a copy of the larger plot?
Sadiq Akbar
on 17 Nov 2022
Walter Roberson
on 17 Nov 2022
@Steven Lord is not here to do your work for you. He is here as a volunteer to help people learn MATLAB. He showed you the technique to use, and now it is up to you to apply the technique for your own situation.
Sadiq Akbar
on 17 Nov 2022
Walter Roberson
on 17 Nov 2022
The best way to learn is by doing. So try to apply the technique he showed. If you encounter an error message that you do not understand, then post again showing your new code and a complete copy of the error message.
Sadiq Akbar
on 17 Nov 2022
Sadiq Akbar
on 17 Nov 2022
Image Analyst
on 17 Nov 2022
0 votes
See my collection of demos attached that put insets within larger axes.
12 Comments
Sadiq Akbar
on 17 Nov 2022
Sadiq Akbar
on 17 Nov 2022
Image Analyst
on 17 Nov 2022
Yeah, because your y value is not called y1 in your .mat file. The mat file loads many variables. You need to figure out which one is y and change variable names accordingly.
Sadiq Akbar
on 19 Nov 2022
Image Analyst
on 19 Nov 2022
That's correct. Why should it look like a histogram? You're not calling the histogram function on any variable.
Sadiq Akbar
on 19 Nov 2022
Image Analyst
on 19 Nov 2022
Then call histogram(), which you are doing in your first program. Or call histcounts() followed by bar(). You don't need to do both histogram and histcounts. What variable would you like the histogram of?
Sadiq Akbar
on 20 Nov 2022
Image Analyst
on 20 Nov 2022
That's a confusing bar chart. Not only do the bars overlap for the different series but the bars are different widths and there are different numbers of bars. I would not advise you to confuse whomever you're going tho show this to by using this confusing display. I would just use three separate charts, or it they are on one chart, then have the bars be side by side and of the same width. It will be more understandable.
Sadiq Akbar
on 20 Nov 2022
Walter Roberson
on 20 Nov 2022
If you have data that is not yet had the bins analyzed, then use histogram()
If you have counts for each bin then call bar(BINLOCATIONS, COUNTS)
Sadiq Akbar
on 20 Nov 2022
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!