Plotting two x axes with one Y-axis, and second x axis are non-ordered numbers
조회 수: 1 (최근 30일)
이전 댓글 표시
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)');
댓글 수: 0
답변 (2개)
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
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
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Axis Labels에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!