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

8 次查看(过去 30 天)
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]);

采纳的回答

Matt J
Matt J 2023-10-24
编辑:Matt J 2023-10-24
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 个评论
Cesar Adolfo Cruz Vargaya
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]);

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Axis Labels 的更多信息

标签

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by