Plotting two x axes with one Y-axis, and second x axis are non-ordered numbers

조회 수: 1 (최근 30일)
bluestar67
bluestar67 2018년 8월 17일
편집: bluestar67 2018년 8월 20일
I am trying to plot mixing depth as a function of day of the year (first x-axis) and the corresponding latitude where measurements were taken for that date. The issue is that the labels of the second axis need to be added text, because they are not always in numerical order. I can get them to plot but they do not align correctly or do not include all the labels for the second x-axis. I use Matlab R2015a.
This is an example of latitudes correspond for each day of year.
N2_Stn_doy = 138 139 140 141 142 143 143 145 146 147 148 150 N2_Stn_Lat= 54.43 56.34 56.23 53.55 53.48 50.23 49.97 47.65 47.54 47.38 47.29 44.11
matrix1=[N2_Stn_doy;N2_Stn_MLD];
h=figure(6) line(matrix1(1,:), matrix1(2,:),'Color','b'); hold on axis([138,150, 0 300]); set(gca, 'YDir', 'reverse') xlabel('day of year'); ylabel ('Depth (m)');
ax1=gca; %ax2 = axes('Position',get(ax1,'Position'),'XAxisLocation','top','YAxisLocation','left',... % 'Color','none','XColor','k','YColor','k'); ax2 = axes('Position',get(ax1,'Position'),'XAxisLocation','top','YAxisLocation','right','Color','none','XColor','k','YColor','k');
k=get(ax1,'XTick'); l=get(ax1,'Position'); set(ax2,'YTick',([])); set(ax2,'Position',l,'Xlim',([44.11 54.43]),'XTickLabel',{'54.43' '56.34' '56.26' '53.54' '53.48' '50.23' '49.97'... '47.65' '47.54' '47.38' '47.29' '44.11'}); %set(ax2,'YTickLabel', num2str(get(ax2,'YTick'),'%.2f\n'),'FontName','Times New Roman','FontSize',24); c=get(ax2,'XLabel'); set(c,'String','Latitude (^{\circ}N)');

답변 (2개)

Honglei Chen
Honglei Chen 2018년 8월 17일
I would replace the code after
ax1 = gca;
with the following
ax2 = axes('Position',get(ax1,'Position'),'XAxisLocation','top','YAxisLocation','right','Color','none','XColor','k','YColor','k');
k=get(ax1,'XTick');
l=get(ax1,'Position');
line(matrix1(1,:), nan(1,size(matrix1,2)),'Parent',ax2);
set(ax2,'YTick',([]));
set(ax2,'XTickLabel',{'54.43' '56.34' '56.26' '53.54' '53.48' '50.23' '49.97'...
'47.65' '47.54' '47.38' '47.29' '44.11'});
%set(ax2,'YTickLabel', num2str(get(ax2,'YTick'),'%.2f\n'),'FontName','Times New Roman','FontSize',24);
c=get(ax2,'XLabel');
set(c,'String','Latitude (^{\circ}N)');
This is essentially your code with an invisible line and then set the axis label to what you have in your code. Is this what you want?
HTH
  댓글 수: 2
Kristina Mojica
Kristina Mojica 2018년 8월 17일
편집: Kristina Mojica 2018년 8월 17일
Thank you for your answer. However
% line(matrix1(1,:), nan(1,size(matrix1,2)),'Parent',ax2);
Did not lead to any changes in the figure.
Honglei Chen
Honglei Chen 2018년 8월 17일
What do you want to plot? I don't see that in your code either. What I did here is to make sure the x axis is properly populated so you can replace the tick label with what you want. You can technically plot anything here as long as you use the desired x values.
HTH

댓글을 달려면 로그인하십시오.


bluestar67
bluestar67 2018년 8월 20일
편집: bluestar67 2018년 8월 20일
Matrix 1 is day of the year and depth [doy; depth]
matrix1 =
138.0000 139.0000 140.0000 141.0000 142.0000 143.0000 143.5000 145.0000 146.0000 147.0000 148.0000 150.0000
14.0118 37.0378 5.5054 22.0171 22.5176 12.0057 16.5077 237.5580 155.2366 7.6018 12.4024 32.4972
This corresponds to the bottom x-axis and y-axis. I want to add a top x-axis with the Latitudes for each of the doy.
A matrix of [doy; Latitude]
Lat_doy =
138.0000 139.0000 140.0000 141.0000 142.0000 143.0000 143.5000 145.0000 146.0000 147.0000 148.0000 150.0000
54.4295 56.3376 56.2644 53.5448 53.4816 50.2292 49.9726 47.6472 47.5413 47.3786 47.2932 44.1085

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by