Troubleshooting Title and Axis Label Placement in Log-Log Plot with Dual Axes in MATLAB Live Script

5 views (last 30 days)
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]);

Accepted Answer

Matt J
Matt J on 24 Oct 2023
Edited: Matt J on 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 Comments
Cesar Adolfo Cruz Vargaya
Cesar Adolfo Cruz Vargaya on 24 Oct 2023
I found the solution, I just needed to change the first axis "position" so it get copied to the second one, while maintaining the figure size and aspect ratio.
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("$%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')
lgd = legend('Location','northwest','Interpreter','latex');
title(lgd,'Ratio','Interpreter','none')
set(gca,'MinorGridLineStyle','-')
set(gca,'TickLabelInterpreter','latex')
% Added this
set(gca,'Position',[0.1 0.15 0.8 0.7]) % [L B W H]
% ---
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]);

Sign in to comment.

More Answers (0)

Categories

Find more on Formatting and Annotation in Help Center and File Exchange

Tags

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!