Method for aligning tick labels

35 次查看(过去 30 天)
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?

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2012-9-14
编辑:Azzi Abdelmalek 2012-9-14
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)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Grid Lines, Tick Values, and Labels 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by