[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

plot2axes problem

Posted on 2009-02-24
8
Medium Priority
?
479 Views
Last Modified: 2013-11-25
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
0
Comment
Question by:James_h1023
  • 4
  • 3
8 Comments
 
LVL 15

Expert Comment

by:yuk99
ID: 23725539
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.
0
 
LVL 4

Author Comment

by:James_h1023
ID: 23732324
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]
0
 
LVL 5

Expert Comment

by:LukeyJay
ID: 23733703
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Author Comment

by:James_h1023
ID: 23733839
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

0
 
LVL 15

Expert Comment

by:yuk99
ID: 23735232
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.

0
 
LVL 4

Author Comment

by:James_h1023
ID: 23737552
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
0
 
LVL 15

Accepted Solution

by:
yuk99 earned 500 total points
ID: 23738554
I confirm that behavior. The small shift happens when you add title to the first plot in the second raw.
The shift go away when I applied title to the secondary axes:

title(ax(2),getOdour(odour),'Color','r');
...
title(ax(2),getOdour(odour));

Also works replacing title with text (looks better, I think):

text((max-min)/2,1,getOdour(odour),'verticalalignment','bottom','horizontalalignment','center','color','r')
...
text((max-min)/2,1,getOdour(odour),'verticalalignment','bottom','horizontalalignment','center')
0
 
LVL 4

Author Comment

by:James_h1023
ID: 23743237
Brilliant thanks very much, all sorted :)
Strange how it only affects when added to the first axes, but nonetheless fixed.

James
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses
Course of the Month17 days, 20 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question