Add 2nd horizontal axis to plot

209 visualizzazioni (ultimi 30 giorni)
Dennis Premoli
Dennis Premoli il 27 Mag 2021
Commentato: Adam Danz il 28 Mag 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
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 Commenti
Adam Danz
Adam Danz il 28 Mag 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
Dennis Premoli il 28 Mag 2021
I have added it now, but keep in mind that the Excel sheet wasn't meant to be read by others...

Accedi per commentare.

Risposta accettata

Adam Danz
Adam Danz il 28 Mag 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.
  4 Commenti
Dennis Premoli
Dennis Premoli il 28 Mag 2021
Modificato: Dennis Premoli il 28 Mag 2021
Thank you very much for the great advice.
"Yuk, this loop is a mess." That was my bad. I had simplified my code to make a clearer example to post here but forgot that the loop wasn't being use anymore. That was to plot 3 graphs (hence the hold on).
I had read about hold all being removed in the future, but mainly just forgot to change it.
Regarding the handles and indexing, that is great advice. It's a very important part of Matlab that I wasn't taught (I mainly use Matalab for data plotting as a materials engineer) so I've been trying to make sense of how/when to properly use them in. I still have a long way to go regarding best practice.
Again, thanks for all the great feedback and the great result below! (now just waiting for Matalab to finally make a xxaxis function.)
Adam Danz
Adam Danz il 28 Mag 2021
Looks good!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Axes Appearance in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by