Removing risidual ticks from second x axis

11 views (last 30 days)
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?

Accepted Answer

Voss
Voss on 6 Mar 2024
Edited: Voss on 6 Mar 2024
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')

More Answers (0)

Categories

Find more on Axes Appearance in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!