Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
multiple y axis

Subject: multiple y axis

From: nman2084

Date: 17 Jun, 2011 16:23:04

Message: 1 of 5

Data is like this:

120 136.68 125.51 6.21
95 136.68 107.58 19.42
70 136.68 122.01 13.27
145 136.68 115.11 11.01
170 136.68 104.64 16.52

I would like to have a bar graph stacked or grouped one y axis should have column 2 and column 3 data, and then another y axis with last column data. x axis is with first column.

Here is the code i tried but not working as it should be:


%% Load the file

[fn,pn] = uigetfile('*.ADCsed2rouseStat', 'Open the ADCP file to be read');
file1=fullfile(pn,fn);
dat1 = load(file1);

x = dat1(:,1);
a = dat1(:,2);
b = dat1(:,3);
c = dat1(:,4);

%%
y = [a,b,c];

bar(x,y,'group');
maxpDiff = max(c);
maxcTSM = max(b);
maxsTSM = max(a);
if maxcTSM > maxsTSM
    maxT = maxcTSM;
else
    maxT = maxsTSM;
end

set(gca,'ytick',0:10:maxT);


axesPosition = get(gca,'Position');
hNewAxes = axes('Position',axesPosition,...
                'Color','none',...
                'YLim',...
                'YAxisLocation','right',...
                'XTick',[],...
                'Box','off');
            
            
            

set(hNewAxes,'ytick',0:10:100);

Subject: multiple y axis

From: dpb

Date: 17 Jun, 2011 17:52:37

Message: 2 of 5

nman2084 wrote:

> Data is like this:
>
> 120 136.68 125.51 6.21
> 95 136.68 107.58 19.42
> 70 136.68 122.01 13.27
> 145 136.68 115.11 11.01
> 170 136.68 104.64 16.52
>
> I would like to have a bar graph stacked or grouped one y axis should
> have column 2 and column 3 data, and then another y axis with last
> column data. x axis is with first column.
...
...

x = dat1(:,1);
a = dat1(:,2);
b = dat1(:,3);
c = dat1(:,4);
y = dat1(:,2:4);
bar(x,y,'group');
rect = get(gca,'Position');


h1 = axes('Position',rect,...
          'Color','none',...

          'YAxisLocation','right',...
          'XTick',[]);



works here to give you the second y-axis.

Use the same number of ticks on left and right axes to make the tick
marks line up and looks like it would do.

I didn't try to see if could overlay two actual bar graphs to physically
associate the last column w/ the righthand axis; I use bar charts so
rarely I don't have any adeptness w/ them in Matlab at all...

Not sure where your troubles were or what you wanted that didn't get,
precisely...

--

Subject: multiple y axis

From: nman2084

Date: 17 Jun, 2011 18:44:20

Message: 3 of 5

I tried this, but the right y axis starts with 0 and no other tick marks are there.

rect = get(gca,'Position');


h1 = axes('Position',rect,...
          'Color','none',...
          'YAxisLocation','right',...
          'XTick',[]);
      
set(gca,'ytick',0:10:maxT);
set(h1,'ytick',0:10:100);

Subject: multiple y axis

From: dpb

Date: 17 Jun, 2011 20:01:14

Message: 4 of 5

nman2084 wrote:

> I tried this, but the right y axis starts with 0 and no other tick marks
> are there.
>
> rect = get(gca,'Position');

h0 = gca; % save first axes for later...

>
>
> h1 = axes('Position',rect,...
> 'Color','none',...
> 'YAxisLocation','right',...
> 'XTick',[]);
> set(gca,'ytick',0:10:maxT);
> set(h1,'ytick',0:10:100);


ylim(h1,[0 100]) % default limits are 0-1

You'll need to set limits as desired for other axis as well...

--

Subject: multiple y axis

From: dpb

Date: 17 Jun, 2011 20:31:37

Message: 5 of 5

dpb wrote:

...


> You'll need to set limits as desired for other axis as well...
...


Really meant you'll want to set same number of ticks for left axis as
right (or at least even multiples) so the right axis tick marks are
coincident to each other for neatness. That probably means changing the
limits as well as the spacing to have "pretty" numbering...

--

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us