Plotting 2 x-axes against 1 y-axis

6 visualizaciones (últimos 30 días)
Molly Monroy
Molly Monroy el 26 de Feb. de 2024
Comentada: Molly Monroy el 27 de Feb. de 2024
Hi,
I am looking to add a second x-axis (measuring time) at the top of the plot seen below. The time corresponds to the number of cycles (approx 275 cycles per day) and should be reaching a maximum value of 20 years (tick mark every 1 or 2 year(s)). I would like to set up the second axis (measuring time) as an array such as: " x2 = 0:1:20; ".I have tried using some of the answers provided to other questions however it is not working for me.
My code for the plot is as follows:
figure(3)
plot(dN, ava)
title('Monte Carlo Simulation 200000','FontSize',14)
xlabel('Number of Cycles')
ylabel('Crack size (mm)')
ylim([0 20])
xlim([0 (total_cycles+50000)])
Thanks!

Respuesta aceptada

Dave B
Dave B el 26 de Feb. de 2024
Editada: Dave B el 26 de Feb. de 2024
You can do something like this by creating a second axes, and in these cases it can be handy to use a tiledlayout to keep the two axes objects aligned. Make sure that you either do you plotting before creating the second axes (if the units of dN match the bottom axis units), or specify ax1/ax2 as the first argument for plot...
ax1=nexttile;
total_cycles = 2e6;
plot(linspace(0, total_cycles, 100), randn(1,100)+10)
xlim([0 total_cycles + 50000])
xlabel('Number of Cycles')
ylabel('Crack size (mm)')
ax2=axes(ax1.Parent, ...
'XAxisLocation','top','Color','none','XLim',[0 20],'XTick',0:2:20);
linkaxes([ax1 ax2],'y') % This is desirable if you want to plot into either axes
ylim([0 20])
title('Monte Carlo Simulation 200000','FontSize',14)
  4 comentarios
Dave B
Dave B el 26 de Feb. de 2024
Ah yes, good point, We can turn off the box to hide the extra tick marks at the top. This will also remove the axis at the right, hopefully that is okay! And we should probably also hide the duplicated y axis, even though you can't really see it.
With respect to your line, you'll want to make sure that you know which of the two axes you're targeting now that there are two. In truth it doesn't matter much because [0 2e6] would work for both, but I think the 'hold on' is out of order.
(You might also consider using yline to draw the line)
ax1=nexttile;
total_cycles = 2e6;
plot(linspace(0, total_cycles, 100), randn(1,100)+10)
xlim([0 total_cycles + 50000])
xlabel('Number of Cycles')
ylabel('Crack size (mm)')
box off
ax2=axes(ax1.Parent, ...
'XAxisLocation','top','Color','none','XLim',[0 20],'XTick',0:2:20);
linkaxes([ax1 ax2],'y') % This is desirable if you want to plot into either axes
ylim([0 20])
title('Monte Carlo Simulation 200000','FontSize',14)
ax2.YColor='none';
xlabel('Time (years)')
hold(ax1,'on')
lineyy = [5 5];
linexx = [0 2e6];
plot(ax1,linexx,lineyy,'k-','Color','r','LineWidth',0.5)
% note you could also do use yline for simplicity:
% yline(ax1, 5, 'r')
Molly Monroy
Molly Monroy el 27 de Feb. de 2024
Ok brilliant thank you, that's working perfectly now!

Iniciar sesión para comentar.

Más respuestas (1)

Aquatris
Aquatris el 26 de Feb. de 2024
I think this is what you are looking for, see the example for Display Data with Multiple x-Axes and y-Axes
  1 comentario
Molly Monroy
Molly Monroy el 26 de Feb. de 2024
I've tried using that but my plot was not displaying anything. Thanks though!

Iniciar sesión para comentar.

Etiquetas

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by