Removing risidual ticks from second x axis

조회 수: 27 (최근 30일)
Andriy Boubriak
Andriy Boubriak 2024년 3월 6일
편집: Voss 2024년 3월 6일
Hi all, I am trying to create a plot with 2 x axis. I want there to be a single plot, but include a second x axis on the top, which contains the same data but for different units.
%x variable for bottom axis
xi=[0, 0.033 ,0.1];
%x variable for top axis
xi_b = [0.0197, 0.06, 0.123];
%variable for the y axis
k = [0.7,0,0];
figure(1)
t = tiledlayout(1,1);
ax1 = axes(t);
%plot 4 lines on first axis
plot(ax1,xi,k,'LineWidth',2)
hold on
%adjust stuff for this graph
set(gca,'FontSize',14)
xlabel('x1','FontSize',14)
ylabel('y1','FontSize',14)
ylim([0 1])
%add second axis at the top
ax2 = axes(t);
ax2.XAxisLocation = 'top';
ax2.YAxisLocation = 'right';
ax2.YAxis.Visible = 'off';
ax2.Color = 'none';
xlabel(ax2,'x2' )
set(ax2,'FontSize',14)
ax2.XLim = [xi_b(1),xi_b(end)];
It produces the following image. As you can see the top x axis has ticks from both the new x axis range, as well as ticks which align with the bottom x axis ticks. How do I remove the ticks at the topc which align the bottom x axis but keep the ones which correspond to the numbers on the top?

채택된 답변

Voss
Voss 2024년 3월 6일
편집: Voss 2024년 3월 6일
You can turn the "box" off for any axes that has it on, which removes the axes lines and ticks from the sides opposite the AxisLocation. This will leave you with an empty right side in this case, which you can then reproduce using an appropriate xline if you want.
%x variable for bottom axis
xi=[0, 0.033 ,0.1];
%x variable for top axis
xi_b = [0.0197, 0.06, 0.123];
%variable for the y axis
k = [0.7,0,0];
figure(1)
t = tiledlayout(1,1);
ax1 = axes(t);
%plot 4 lines on first axis
plot(ax1,xi,k,'LineWidth',2)
hold on
%adjust stuff for this graph
set(gca,'FontSize',14)
xlabel('x1','FontSize',14)
ylabel('y1','FontSize',14)
ylim([0 1])
%add second axis at the top
ax2 = axes(t);
ax2.XAxisLocation = 'top';
ax2.YAxisLocation = 'right';
ax2.YAxis.Visible = 'off';
ax2.Color = 'none';
xlabel(ax2,'x2' )
set(ax2,'FontSize',14)
ax2.XLim = [xi_b(1),xi_b(end)];
box(ax1,'off')
xline(ax2,xi_b(end),'k')

추가 답변 (0개)

카테고리

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

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by