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)');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/194007/image.jpeg)
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!