Add 2nd horizontal axis to plot

65 views (last 30 days)
Dennis Premoli
Dennis Premoli on 27 May 2021
Commented: Adam Danz on 28 May 2021
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
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.
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;
Dennis Premoli
Dennis Premoli on 28 May 2021
I have added it now, but keep in mind that the Excel sheet wasn't meant to be read by others...

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 28 May 2021
To create axes using tiledlayout you should use nexttile to create the primary axes (see this answer).
To create multiple x-tick-labels you've got a few options.
If you want the x-ticks on top an easier solution would be use text() to add labels above the top of the axes at the locations of the xticks. The down-side of this approach is that the text object are anchored to data units so if there are any changes to the axis limits the text objects may move with the data rather than staying achored to the axis ticks. There are ways around that such as setting callback functions or listeners to adjust the text label positions in the event of an axis limit change but that starts to get complicated, but achievable.
Another relatively easy approach that avoide that problem is use use multiple rows of x-tick-labels as shown in these answers
Lastly, you could add a second pair of axes on top of the primary axes which is your current approach but requires some additional work to ensure that an adjustment in axis limits will not decouple the two axes. This answer demonstrates that process using subplot but the same approach can be used with tiledlayout with a few minor changes. Consider using subplot to make things easier. If you use tiledlayout, the position properties cannot be set with TiledLayout and you'll receive warnings when that happens. You can modify the demo (2 lines will need modified - you'll see warnings otherwise).
If you have any trouble, share your updated code, any files or variables needed to run the code, and an updated screenshot would be helpful.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!