Removing risidual ticks from second x axis
12 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Andriy Boubriak
el 6 de Mzo. de 2024
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?
0 comentarios
Respuesta aceptada
Voss
el 6 de Mzo. de 2024
Editada: Voss
el 6 de Mzo. de 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')
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Axes Appearance en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

