Why does histogram gives issues? I rephrased my question

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

Thank you very much dear Steven Lord for your help. But you didn't plot my data. You have taken your own data. Further, there is no preview inside the given graph.
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?
The preview makes the graph more presentable. Also I have given you 3 sets of fitness data but you have taken a single data of your own. Kindly do it with my 3 sets of data with preview. Regards,
@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.
Thank you very much dear Walter Roberson for your information. Actually I am not so much expert in Matlab though I try my level best in its learning. As you can see whenever I fell a difficulty, I ask a question on this forum. Thanks once again and sorry I didn't see your post earlier.
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.
I wrote the code according to Steven as follows:
clear all
clc
load 2sn35 %x = rand(1, 1e5)*1e-5;
x1=sort(one,'descend');
fitness2sn35=x1;
load 2sn45 %x = rand(1, 1e5)*1e-5;
x2=sort(one,'descend');
fitness2sn35=x2;
ax = axes;
%Specify bins edges with logarithmic spacing.
h = histogram(ax, x1, 10.^(-10:-5));
h = histogram(ax, x2, 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';
But it gives me an empty figure widow.

Sign in to comment.

See my collection of demos attached that put insets within larger axes.

12 Comments

Thank you very much dear Image Analyst for your detailed codes. Indeed I liked them very well as they are very step-by-step and with comments. But the problem is that I want to draw all the three sets of MY fitness data on a single graph with its preview as is clear from the attachment. so if you can help in this regard.
Regards,
I tried your code on my data as follows:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
fontSize = 18;
load 2sn35
one=sort(one,'descend');
x1=one; % x1 = linspace(0, 1);
load 2sn45
one=sort(one,'descend');
x2=one;%x2 = linspace(3/4, 1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% I don't have y1 and y2. But my data on y-axis should be the strength in
% each data between 1 and 100 of the fitness values that how many those
% values are there in each fitness data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % y1 = sin(2*pi*x1);
% % y2 = sin(2*pi*x2);
figure(1)
% plot on large axes
plot(x1, y1, 'LineWidth', 2)
grid on;
ax1 = gca; % Store handle to axes 1.
% Create smaller axes in top right, and plot on it
% Store handle to axes 2 in ax2.
ax2 = axes('Position',[.7 .7 .2 .2])
box on;
plot(x2, y2, 'b-', 'LineWidth', 2)
grid on;
% Now draw something back on axis 1
hold(ax1, 'on'); % Don't blow away existing curve.
y1b = cos(2*pi*x1/3);
plot(ax1, x1, y1b, 'r-', 'LineWidth', 2);
% Now draw something back on axis 2
hold(ax2, 'on'); % Don't blow away existing curve.
y2b = cos(2*pi*x2/3);
plot(ax2, x2, y2b, 'r-', 'LineWidth', 2);
but it gives me an empty figure window and the following eror:
Unrecognized function or variable 'y1'.
Error in histoImageAnalyst1 (line 24)
plot(x1, y1, 'LineWidth', 2)
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.
Thanks a lot dear Image Analyst for your kind response. I re-wrote your code as below:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
load 2sn35
one=sort(one,'descend');
x1=one; % x1 = linspace(0, 1);
load 2sn45
one=sort(one,'descend');
x2=one;%x2 = linspace(3/4, 1);
% % y1 = sin(2*pi*x1);
% % y2 = sin(2*pi*x2);
y1=1:100;
y2=y1;
figure(1)
% plot on large axes
plot(x1, y1, 'LineWidth', 2)
hold
plot(x2,y2,'LineWidth', 2)
grid on;
ax1 = gca; % Store handle to axes 1.
ax1.XScale= 'log'
Npw it runs and works but the graph doesn't seem like the one in the attachment. Its just a simple plot and not a histogram.
That's correct. Why should it look like a histogram? You're not calling the histogram function on any variable.
Thanks for your kind response.If you look at my posted question, its about histogram.So I want to plot its histogram plot not ordinary plot.
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?
Thanks for your kind response. Yes, I am doing in my posted code but as I have said that I don't get the graph like the one in the attachment.
I want to make a histogram of the variable "one". When we load 2sn35, the variable one is there. Likewise, when we load 2sn45, again one is there. But how to make its histogram to be like the one in the attachment?
Regards,
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.
Thanks for your kind response. Doesn't matter. You are requested just to write the code here so that I can check it myself. May be I can suggest some change.
Regards,
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)
Thank you very much dear Walter Roberson for your kind response. Each variable "one" has 100 different values. So if we show the x-axis as logrithmic and the y-axis will show count values from 1 to 100. Actually someone said to me use "histcounts" and "histogram" and then he gave me the above code, but I don't understand it and also it doesn't give me the graph like the one in the attachment. I don't understand what does histcounts does and what does histogram does? And why do we write [~,edges1] as output arguments in hiscounts but doesn't write some output arguments in histogram. Further, x-axis is also not logrithmic like 10^-1, 10^-2, 10^-3,.....10^-10 etc.

Sign in to comment.

Categories

Asked:

on 16 Nov 2022

Commented:

on 20 Nov 2022

Community Treasure Hunt

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

Start Hunting!