Add 2nd horizontal axis to plot
Show older comments
Hey everyone,
I'm trying to plot 3 lines onto a single graph, but I'd like to have a second xaxis on the top since I'm dealing with ratios here.
This is what I got up to now and it's the base graph.

I've then tried to add a second xaxis using the tiledlayout (t = tiledlayout(1,1)) approach suggested on Matalab documentation (cause xxaxis doesn't exist ugh!!!)
but when I do that I can't seem to use any sort of hold command to plot more than one graph at the same time, so I end up with this below. (Also, for some reason the text seems to be of very low quality and the legend also seems to die when tilelayout is involved.)

%% Data Import
T = readtable('Sample Compositions - Copy.xlsx');
markers = {'-ok','-sk','-^k'};
%% Ni Ratio
figure
k=1; %Marker counter
NiCoRatios = [T.Ni(1),T.Ni(4),T.Ni(7),T.Ni(2),T.Ni(5),T.Ni(8),T.Ni(3),T.Ni(6),T.Ni(9)]';
NiCoP = [T.P(1),T.P(4),T.P(7),T.P(2),T.P(5),T.P(8),T.P(3),T.P(6),T.P(9)]';
T1 = table(NiCoRatios,NiCoP);
t = tiledlayout(1,1);
for i = 1:3:9
j = i+2;
ax1 = axes(t);
hold all % if I keep this & tilelayout, the output will be completely broken.
plot(ax1,T1.NiCoRatios(i:j),T1.NiCoP(i:j),sprintf('%s',markers{k}))
ax2 = axes(t);
ax2.XAxisLocation = 'top';
ax2.YAxisLocation = 'right';
ax2.Color = 'none';
ax2.XDir = 'reverse';
xlim (ax1,[15,60])
ylim (ax1,[0,7])
xlim (ax2,[15,60])
ylim (ax2,[0,7])
grid on
xlabel(ax1,'Ni at.%')
xlabel(ax2,'Co at.%')
ylabel(ax1,'Oxidation Rate Cte. \itk')
ylabel(ax2,'Oxidation Rate Cte. \itk')
legend('Al:Ti 1:3','Al:Ti 1:1','Al:Ti 3:1','Location','Best Outside')
k =k+1;
end
2 Comments
Adam Danz
on 28 May 2021
We can't run your code because we do not have access to the xlsx file. You could attach it to your question so we can reproduce the problems.
Dennis Premoli
on 28 May 2021
Accepted Answer
More Answers (0)
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
