Plotting 2 x-axes against 1 y-axis

9 次查看(过去 30 天)
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!

采纳的回答

Dave B
Dave B 2024-2-26
编辑:Dave B 2024-2-26
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 个评论
Dave B
Dave B 2024-2-26
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 2024-2-27
Ok brilliant thank you, that's working perfectly now!

请先登录,再进行评论。

更多回答(1 个)

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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Geographic Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by