Troubleshooting Title and Axis Label Placement in Log-Log Plot with Dual Axes in MATLAB Live Script
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Cesar Adolfo Cruz Vargaya
el 24 de Oct. de 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 comentarios
Respuesta aceptada
Matt J
el 24 de Oct. de 2023
Editada: Matt J
el 24 de Oct. de 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 comentarios
Matt J
el 24 de Oct. de 2023
I'm glad, but if the problems are now solved, please Accept-click the answer.
Más respuestas (0)
Ver también
Categorías
Más información sobre Formatting and Annotation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!