Adding a secondary x axis on the top of a line plot

조회 수: 32 (최근 30일)
SKP
SKP 2021년 9월 21일
댓글: SKP 2021년 9월 24일
In the line plot obtained after running the code appended below, xticks are to be added at the top such that their value is 50 times the xticks at the bottom of the plot. Moreover, an xlabel needs to be at the top also. Could someone help me with the code.
I had tried to implement some suggestions in previous posts on the topic, but somehow I get misaligned and overlapping axes. Thanks in advance.
x = 15:15:1800;
y1 = x./(1-x);
y2 = exp(-x./(1+x));
figure;
tiledlayout(1,1);
nexttile;
yyaxis left; plot(x/360,y1,'-bo'); hold on;
ylabel('PCC'); xlabel('time (days)');
yyaxis right; plot(x/360,y2,'-xr'); % hold on;
ylabel('DPCC (mm)'); % xlabel('time (days)');
axis square;
legend('PCC','DPCC','Location','East');

채택된 답변

Prachi Kulkarni
Prachi Kulkarni 2021년 9월 24일
Hi,
Following is a modified version of your code which will give you the secondary X axis at the top of the plot with an xlabel as required.
x1 = 15:15:1800;
y1 = x1./(1-x1);
y2 = exp(-x1./(1+x1));
x2 = 50*x1;
t = tiledlayout(1,1);
ax1 = axes(t);
l1 = plot(ax1,x1,y1,'-bo');
ax1.Color = 'none';
xlabel('time (days)'); ylabel('PCC');
axis square
ax2 = axes(t);
l2 = plot(ax2,x2,y2,'-xr');
ax2.XAxisLocation = 'top';
ax2.YAxisLocation = 'right';
ax2.Color = 'none';
xlabel('Label for top X axis'); ylabel('DPCC (mm)');
axis square
legend([l1;l2],'PCC','DPCC','Location','East');
ax1.Box = 'off';
ax2.Box = 'off';
For more information, please refer to the following documentation on adding multiple X and Y axes to your plots.
  댓글 수: 1
SKP
SKP 2021년 9월 24일
Thank you so much. This is precisely what I wanted.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Axes Appearance에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by