How to have two plot in the same, each plot is the rusult of a specific function
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
This is the code in the main function
figure
tiledlayout(2,1)
nexttile
%Plot superiore, Tensione di vapore calcolata a TMax e TMin
plotDoppioAsseX(VetTempAssegnate(:,1), VettoreTenVap2(:,1),stringVetTempAssegnate1,stringVettoreTenVap2,VetTempAssegnate(:,2),VettoreTenVap2(:,2),stirngVetTempAssegnate2,stringVettoreTenVap2)
nexttile
%Plot inferiore
plotDoppioAsseX(VetTempAssegnate(:,1), VettoreTCalcolate2,stringVetTempAssegnate1,stringVettoreTCalcolate2,VetTempAssegnate(:,2),VettoreTCalcolate2,stirngVetTempAssegnate2,stringVettoreTCalcolate2)
This is the called function
function []=plotDoppioAsseX2(compAsseX1, compAsseY1,labelX1,labelY1,compAsseX2,compAsseY2,labelX2,labelY2)
t = tiledlayout(1,1);
ax1 = axes(t);
plot(ax1,compAsseX1,compAsseY1,'-r')
xlabel(labelX1)
ylabel(labelY1)
ax1.XColor = 'r';
ax1.YColor = 'r';
ax2 = axes(t);
plot(ax2,compAsseX2,compAsseY2,'-k')
title(strcat(labelX1,' e ',labelX2,' vs. ',labelY2))
grid on
xlabel(labelX2)
ylabel(labelY2)
ax2.XAxisLocation = 'top';
ax2.YAxisLocation="right";
ax1.Box = 'off';
ax2.Box = 'off';
end
The error when I run the main function is:
"Error using nexttile
The layout does not have sufficient space."
What I'm trying to do is to show a figure with two plots. Each on those have two x axes and two y axes.
I used the funtion "plotDoppioAsseX2" to create a model to plot two x axes and two y axes. So I passed x,y, label,ect. as parameters.
Then I called twice the funtion "plotDoppioAsseX2" to create the two plots to add at the figure.
I don't know if this is the best way to do this. Let me know if you something better.
0 commentaires
Réponses (1)
Pratik
le 19 Nov 2024
Hi Matteo,
I understand that there is an error of
Error using nexttile
The layout does not have sufficient space.
in the above code.
This is happening because the code is trying plot 2 plots in 1x1 grid space as created in the function "plotDoppioAsseX2".
To fix this error, use 'gca' to use the current axis, please refer to the following code snippet of updated "plotDoppioAsseX2" function:
% Revised plotDoppioAsseX2 Function
function plotDoppioAsseX2(compAsseX1, compAsseY1, labelX1, labelY1, compAsseX2, compAsseY2, labelX2, labelY2)
ax1 = gca; % get current axis
plot(ax1,compAsseX1,compAsseY1,'-r')
xlabel(labelX1)
ylabel(labelY1)
ax1.XColor = 'r';
ax1.YColor = 'r';
% Create the second set of axes in the same position
ax2 = axes('Position', ax1.Position, 'Color', 'none');
plot(ax2,compAsseX2,compAsseY2,'-k')
title(strcat(labelX1,' e ',labelX2,' vs. ',labelY2))
grid on
xlabel(labelX2)
ylabel(labelY2)
ax2.XAxisLocation = 'top';
ax2.YAxisLocation="right";
ax1.Box = 'off';
ax2.Box = 'off';
end
Please refer to the following documentation for more information on 'gca' function:
0 commentaires
Voir également
Catégories
En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!