KatyB
KatyB

Reputation: 3990

Linking ticks in a MATLAB plot

Is there a method of aligning tick labels?

I have a figure that has two y axes where the values vary greatly. I would like to align the tick labels so that each value shown on one y label matches up with a value on the opposite ylabel. For example:

data1 = 1+ (12-1).*rand(365,1);
data2 = 1 + (700-1).*rand(365,1);
time = 1:365;
figure(1);
ax1 = axes('position',[0.05 0.5 0.22 0.37]);
plot(time,data1,'k','linewidth',1);
ylabel('label 1');
pos=double(get(ax1,'position'));
ax2=axes('position',pos,'color','none','YAxisLocation','right','xtick',[])
hold on;
plot(time,data2,'r','linewidth',1,'parent',ax2);
ylabel(ax2,'label 2');

Here I would like the second y axis to have the same number of ticks as the first y axis as well as the same spacing between them. How can I achieve this?

Upvotes: 1

Views: 693

Answers (2)

Pursuit
Pursuit

Reputation: 12345

You can set the y axis limits and tick locations explicitly:

ylim(ax1,[lowerBound upperBound])
set(ax1,'ytick',[tick1 tick2 tick3 tick4])

This can let you fine tune the tick locations for a particular plot. It makes zooming and panning less functional, since the ticks are often left behind.

Upvotes: 0

slayton
slayton

Reputation: 20319

Use plotyy instead of plot, it handles this for you:

plotyy(time, data1, time, data2);

Upvotes: 1

Related Questions