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]);
댓글 수: 0
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Axis Labels에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!