Method for aligning tick labels

72 views (last 30 days)
Is there a method of aligning tick labels? I have a figure that has two y axis 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 yaxis to have the same number of ticks as the first yaxis as well as the same spacing between them. How can I achieve this?

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 14 Sep 2012
Edited: Azzi Abdelmalek on 14 Sep 2012
add this code
data1m=get(ax1,'ylim');
data2m=get(ax2,'ylim')
nt=5; %tick number
data1_tick=linspace(data1m(1),data1m(2),nt);
data2_tick=linspace(data2m(1),data2m(2),nt);
set(ax1,'ytick',round(data1_tick*100)/100)
set(ax2,'ytick',round(data2_tick*100)/100)

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by