Troubleshooting Title and Axis Label Placement in Log-Log Plot with Dual Axes in MATLAB Live Script
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Cesar Adolfo Cruz Vargaya
le 24 Oct 2023
Modifié(e) : Matt J
le 24 Oct 2023
I am in the process of creating a log-log plot and need help with several aspects to ensure accuracy and efficiency in my implementation. The plot requires two x-axes and y-axes, each with unique labels, ticks, and tick labels, as well as a specific title. Although I have managed to plot most elements correctly, the label of the upper x-axis and the plot title are not displaying properly, which might be due to setting a custom figure size using the 'set' function. I am attaching the figure for reference and would appreciate feedback on any potential bad practices or inefficiencies in my code, as well as guidance on how to display grid lines as solid gray lines instead of the default dotted lines. Here is the code snippet for your review:
h = figure();
param = [0.3287,3.5103,1
0.3287,4.5491,2
0.3279,6.0150,4
0.3284,8.3565,10];
x = 10.^(1:6);
for i=1:4
y = x.^(param(i,1))/param(i,2);
dispName = sprintf("$G.R. = %d :1$",param(i,3));
loglog(x,y,'DisplayName',dispName,'LineWidth',1.5), hold on
end
hold off, grid("on"), xlim([1e2 1e6]), ylim([1e-1 1e2])
colororder(gca,"gem12")
xticks(10.^(1:6)), yticks(10.^(-1:2))
xticklabels(arrayfun(@(x) sprintf("%d",x),10.^(1:6)))
yticklabels(arrayfun(@(x) sprintf("%g",x),10.^(-1:2)))
xlabel("Pinion torque, lb in",'Interpreter','latex')
ylabel("Pinion pitch diameter, in",'Interpreter','latex')
legend('Location','northwest','Interpreter','latex')
set(gca,'TickLabelInterpreter','latex')
ax1 = gca;
ax1pos = ax1.Position;
ax2 = axes('Position',ax1pos,'XAxisLocation','top','YAxisLocation','right', ...
'Color','none','XScale','log','YScale','log');
xlim([1e2 1e6]), ylim([1e-1 1e2])
xticks(10.^(1:6)), yticks(10.^(-1:2))
xticklabels(arrayfun(@(x) sprintf("%g",x),0.113*10.^(1:6)))
yticklabels(arrayfun(@(x) sprintf("%g",x),25.4*10.^(-1:2)))
xlabel("Pinion torque, Nm",'Interpreter','latex')
ylabel("Pinion pitch diameter, mm",'Interpreter','latex')
title('Graph')
set(gca,'TickLabelInterpreter','latex')
set(h,'Position',[0 0 700 400]);
0 commentaires
Réponse acceptée
Matt J
le 24 Oct 2023
Modifié(e) : Matt J
le 24 Oct 2023
I've marked lines I've added changed below in comments:
h = figure();
param = [0.3287,3.5103,1
0.3287,4.5491,2
0.3279,6.0150,4
0.3284,8.3565,10];
x = 10.^(1:6);
for i=1:4
y = x.^(param(i,1))/param(i,2);
dispName = sprintf("$G.R. = %d :1$",param(i,3));
loglog(x,y,'DisplayName',dispName,'LineWidth',1.5), hold on
end
hold off, grid("on"), xlim([1e2 1e6]), ylim([1e-1 1e2])
set(gca,'GridLineStyle','-') %<---Matt J
set(gca,'MinorGridLineStyle','-') %<---Matt J
colororder(gca,"gem12")
xticks(10.^(1:6)), yticks(10.^(-1:2))
xticklabels(arrayfun(@(x) sprintf("%d",x),10.^(1:6)))
yticklabels(arrayfun(@(x) sprintf("%g",x),10.^(-1:2)))
xlabel("Pinion torque, lb in",'Interpreter','latex')
ylabel("Pinion pitch diameter, in",'Interpreter','latex')
legend('Location','northwest','Interpreter','latex')
set(gca,'TickLabelInterpreter','latex')
ax1 = gca;
ax1pos = ax1.Position;
ax2 = axes('Position',ax1pos,'XAxisLocation','top','YAxisLocation','right', ...
'Color','none','XScale','log','YScale','log');
xlim([1e2 1e6]), ylim([1e-1 1e2])
xticks(10.^(1:6)), yticks(10.^(-1:2))
xticklabels(arrayfun(@(x) sprintf("%g",x),0.113*10.^(1:6)))
yticklabels(arrayfun(@(x) sprintf("%g",x),25.4*10.^(-1:2)))
xlabel("Pinion torque, Nm",'Interpreter','latex')
ylabel("Pinion pitch diameter, mm",'Interpreter','latex')
title('Graph')
set(gca,'TickLabelInterpreter','latex')
set(h,'Position',[0 0 800 800]); %<---Matt J
3 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Formatting and Annotation 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!