Link to home
Start Free TrialLog in
Avatar of Member_2_2394978
Member_2_2394978Flag for United Kingdom of Great Britain and Northern Ireland

asked on

plot2axes problem

Hi,

Following up from a previous question, I am using plot2axes (http://www.mathworks.com/matlabcentral/fileexchange/7426) to plot two axes. However, My axes, and data, are not inline properly: they are slightly out (see image).

In the first image, you can see the left hand scale is slightly off from the right hand scale; I would like both to be like the right hand scale. Furthermore, in the second image, you can see how the data are being plotted off slightly (these should start at the same value).

In the code snippet, h1 and h4 are the data.

Any suggestions?
James

Edit1: Sorry I think I should clarify my images; in image 1 the two lines should not be starting at the same place; although in image 2 they should.
% Plotting
        [ax, h] = plot2axes(centres,h1);
            axis(gca,[min max 0 1]);
            set(gca, 'YTick', [0 0.5 1]);
            if significant(or,odour)==1
                set(gca,'YTickLabel',['0|' num2str(trialRef(or,odour)/2) '|' ...
                    num2str(trialRef(or,odour))]);
            else
                set(gca,'YTickLabel',['0|' num2str(numel(noise{or})/2) '|' ...
                    num2str(numel(noise{or}))]);
            end
        
        [ax, h] = plot2axes(centres,h4,'--');
            axis(ax(2),[min max 0 1]);
            set(ax(2), 'YTick', [0 0.5 1]);
            set(ax(2), 'YTickLabel', '0|0.5|1');

Open in new window

experts1.bmp
experts2.bmp
Avatar of yuk99
yuk99
Flag of United States of America image

Would you please provide the data as well? You can either make it a code (h1=[..];) or save them into a mat-file and attach here.
Avatar of Member_2_2394978

ASKER

Below are h1 and h4. Strangely, although, when testing these variables quickly to see if they were the correct ones, I plotted them and the axes lined up. However, I was plotting using the whole figure, where before - and as in the attached figures - I was plotting as a subplot(4,5,i); i'm thinking this has something to do with it.

Thanks
James

h1 = [0.571428571428571      0.166666666666667      0.119047619047619      0.0952380952380952      0.0476190476190476      0      0      0      0      0      0      0      0      0      0      0]

h4 = [0.399736812362489      0.295919710982734      0.175760430786301      0.0837559728774998      0.0320226989000242      0.00982307683533402      0.00241760093709347      0.000477385424032789      7.56312292047454e-05      9.61348002268866e-06      9.80409616499697e-07      8.02198714581523e-08      5.26627854369137e-09      2.77378729998949e-10      1.17216823930495e-11      3.97424108808684e-13]
Avatar of LukeyJay
LukeyJay

Hi, James,

What are the values in the variable "centres"?  Just an indexing, but where do the values start and how do they progress?

Luke
Hi,

Please see attached: so centres = [5      15      25      35      45      55      65      75      85      95      105      115      125      135      145      155]

James
binSize = 10;
min = 0;
max = 160;
centres = binSize/2:binSize:max;

Open in new window

James_h102, I'm sorry, but when you are asked for the data, it means ALL the data needed to reproduce your code. We cannot all the time ask for every variable in the code. The basic rule is: Do "clear all" and try to run your code before posting it.

I would advice you to read documentation for plot2axes and use YLoc and YScale properties. YScale would be ratio between your scales (I hope their dependence is linear, otherwise this function will not work). Why are you using gca, not ax(1)? You are manually setting position of axis, probably this is a source of the problem.

My apologies, attached is full code, and data (rename to .mat)  required.

If the code attached is run, the left axes is down a bit; however, if you run the code from plotting start to end, with just one plot for a complete figure itis fine. I'm thinking perhaps it is because i have lots of subplots and they are squashing down the labels of the axes.
% Probability distribution functions
home; clear all; load probs;
numTrials = 18;
numOdours = 18;
 
% Produce matrices
% for trial=1:numTrials;
%     for or=1:1%5
%         for odour=1:numOdours
%             probs(odour,trial,or) = data1(or,odour,trial);
%         end
%     end
% end
 
% PDFs
binSize = 10;
min = 0;
max = 160;
centres = binSize/2:binSize:max;
 
for or=1:1%5
    figure(8+or); clf;
    for odour=1:numOdours
        subplot(4,5,odour); hold all;
        % If the odour/or combination is significant then produce distribution as usual!
        if significant(or,odour)==1
            values = probs(odour,1:trialRef(or,odour),or);
        else % produce noise distribution for this or, regardless of odour (as not significant).
            values = noise{or};
        end
        
        % Histogram
        h1 = hist(values,centres);
        % Gaussian
        [mu, sigma] = normfit(values);
        h2 = normpdf(centres,mu,sigma);
        % Poisson
        [lambda] = poissfit(values);
        h3 = poisspdf(centres,lambda);
        % Truncated Gaussian
            % NOTE: A value of 0, for the truncation point produces NaNs
            % and infinite responses from the MLE function.
        pdf_truncnorm = @(x,mu,sigma) ( normpdf(x,mu,sigma) ./ (1-normcdf(-5,mu,sigma)) );
            % Because the sample is not too far away from the population 
            % (the truncation is not too extreme),
            % the sample mean and stds will probably suffice for the start
            % estimates.
        start = [mean(values),std(values)];
            % Needs time to converge
        options = statset('MaxIter',10000, 'MaxFunEvals',10000); 
            % Get MLE to produce estimates of mu and sigma
        paramEsts = mle(values, 'pdf',pdf_truncnorm, 'start',start, ...
            'lower',[-Inf 0], 'options',options);
        h4 = pdf_truncnorm(centres,paramEsts(1),paramEsts(2));
        
        % Normalise
        h1 = h1/sum(h1);
        h2 = h2/sum(h2);
        h3 = h3/sum(h3);
        h4 = h4/sum(h4);
        % Plotting start
        [ax, h] = plot2axes(centres,h1);
            axis(gca,[min max 0 1]);
            set(gca, 'YTick', [0 0.5 1]);
            if significant(or,odour)==1
                set(gca,'YTickLabel',['0|' num2str(trialRef(or,odour)/2) '|' ...
                    num2str(trialRef(or,odour))]);
            else
                set(gca,'YTickLabel',['0|' num2str(numel(noise{or})/2) '|' ...
                    num2str(numel(noise{or}))]);
            end
        
        [ax, h] = plot2axes(centres,h4,'--');
            axis(ax(2),[min max 0 1]);
            set(ax(2), 'YTick', [0 0.5 1]);
            set(ax(2), 'YTickLabel', '0|0.5|1');
        % Plotting end
            
        if significant(or,odour)==1
            % red title! is a 'significant' response
            title(getOdour(odour),'Color','r');
            % Test
            values=values*(1/std(values));
            values=values-mean(values);
            [h p] = kstest(values);
            if h
                text(20,0.75,['fail ' num2str(p)]);
            else
                text(20,0.75,['pass ' num2str(p)]);
            end
        else
            % normal colour title
            title(getOdour(odour));
        end
        % Save for later use
        pdfs(odour,or) = {h4};
    end
end

Open in new window

probs.txt
ASKER CERTIFIED SOLUTION
Avatar of yuk99
yuk99
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Brilliant thanks very much, all sorted :)
Strange how it only affects when added to the first axes, but nonetheless fixed.

James