319 views (last 30 days)

Show older comments

Hi,

I am using addaxis to plot multiple y axes (first pic), but is there a way to have them all appear on one side (say the right hand side). Is there a better function to do this than addaxis?

If I do this:

figure

plot(x1,y1);

addaxis(x2,y2);

set(gca,'yaxislocation','right');

I get the axes on top of each other.

Thanks

Adam Danz
on 2 May 2020

Edited: Adam Danz
on 4 May 2020

I briefly looked at the addaxis function on the file exchange and I'm not sure it's using the best approach.

Follow this demo below to produce the figure at the bottom. I suggest stepping through the code line by line and reading the comments so you can understand what each line is doing. The order of what's happening is important. If some of the axis alterations are made before plotting the data, the alterations will not be implemented correctly.

The most important part is scaling the axis ticks from the 2nd axis so they align with the axis ticks of the 1st axes. Otherwise you'll have different sets of tick locations which makes the plot very difficult to read.

% Create two identical overlaying axes

fig = figure();

ax1 = axes();

ax2 = copyobj(ax1, fig);

% Link the x axes

linkaxes([ax1,ax2],'x')

% Choose colors for the 2 y axes.

axColors = [

0 0.39063 0; % dark green

0.53906 0.16797 0.88281]; % blue violet

% Do plotting (we'll adjust the axes and the ticks later)

plot(ax1, 1:100, sort(randn(1,100)*100), '-', 'Color', axColors(1,:));

plot(ax2, 1:100, sort(randn(1,100)*60), '-', 'Color', axColors(2,:));

% Set axes background color to transparent and turns box on

set([ax1,ax2],'Color', 'None', 'Box', 'on')

% Set y-ax to right side

set([ax1,ax2], 'YAxisLocation', 'right')

% make the axes 10% more narrow

set([ax1,ax2], 'Position', ax1.Position .* [1 1 .90 1])

% set the y-axis colors

ax1.YAxis.Color = axColors(1,:);

ax2.YAxis.Color = axColors(2,:);

% After plotting set the axis limits.

ylim(ax1, ylim(ax1));

ylim(ax2, ylim(ax2));

% Set the y-ticks of ax1 so they align with yticks of ax2

% This also rounds the ax1 ticks to the nearest tenth.

ax1.YTick = round(linspace(min(ax1.YTick),max(ax1.YTick),numel(ax2.YTick)),1);

% Extend the ax1 y-ax ticklabels rightward a bit by adding space

% to the left of the label. YTick should be set first.

ax1.YTickLabel = strcat({' '},ax1.YTickLabel);

% Set y-ax labels and adjust their position (5% inward)

ylabel(ax1,'y axis 1')

ylabel(ax2','y axis 2')

ax1.YLabel.Position(1) = ax1.YLabel.Position(1) - (ax1.YLabel.Position(1)*.05);

ax2.YLabel.Position(1) = ax2.YLabel.Position(1) - (ax2.YLabel.Position(1)*.05);

Adam Danz
on 17 Dec 2020

I suggest starting a new question since this is off topic from the original question in this thread.

Tommy
on 1 May 2020

It looks like that function flips back and forth between adding the new axes to the left and adding them to the right. How about calling the function three times and deleting the second set of axes after the third has been added?

plot(1:10, rand(10,1));

addaxis(NaN,NaN);

addaxis(1:10, rand(10,1));

axh = getaddaxisdata(gca,'axisdata');

delete(axh{1})

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

Start Hunting!