How to smooth edges of the graph plot and axis range numbering adjustments?
4 次查看(过去 30 天)
显示 更早的评论
Hello All,
From the attached coding i generated 3 different figure plots and in each one of them i wanted the following help to get resolved:
- For figure(1) plot - change my logscale x axis as similar to the one shown in Manoeuvres Rate Plot.png
- For figure(2) plot - change my y-axis with extended decimal format generated from the attached code rather than in exponential form and the x-axis to be changed as similar to one shown in Risk Collision Plot1.png. Also please help to smooth the sharp edges to have a smoother plot without loosing its accuracy.
- For figure(3) plot - change my y-axis with extended decimal format generated from the attached code rather than in exponential form similarly done for figure (2) plot and change my logscale x axis as similar to the one shown in Manoeuvres Rate Plot.png. Also please help to smooth the sharp edges to have a smoother plot without loosing its accuracy.
Please kindly support on resolving this
Thanks in advance !
0 个评论
回答(1 个)
Yash
2023-9-4
编辑:Yash
2023-9-4
In MATLAB, you can control the format of tick labels on the x-axis (or y-axis) by customizing the axis properties.
Figure 1:
If you want to change the tick label format to scientific notation with "1e-06" instead of "10^-6," you can use "sprintf" function to set the desired format.
Please refer to the below link for documentation of “sprintf” function:
Here's how you can do it:
figure (1);
semilogx(x,y1,'Linewidth',1);
legend({'Manoeveur Rate'},'EdgeColor','none')
xlabel('ACPL');
ylabel('Mean number of Avoidance Manoevres');
set(gcf,'color','w');
xticklabels(arrayfun(@changeFig1XLabel, xticks, 'UniformOutput', false));
function n = changeFig1XLabel(label)
if label < 1e-04
n = sprintf('%.0e', label);
else
n = sprintf('%.4g', label);
end
end
The "arrayfun" function is used to format the tick labels as strings in scientific notation with the help of "changeFig1XLabels" which generates the desired string based on the numerical value passed to the function. "xticklabels" is used to customize the tick values and labels.
For more information on “xticklabels”, refer to the following documentation:
Figure 2:
If you want the y-axis ticks to be inline with the one's generated from the code you could use the axis properties of Y-axis to change the exponet value to the desired one and to change the X-Axis labels you can use the "semilogx" function to get the tick labels in powers of 10. By this way your graph can be smoother edges.
For more information on “xticklabels”, refer to the following documentation:
Here's how you can do it:
figure (2);
semilogx(y1,y2,'Linewidth',1);
hold on
semilogx(y1,y4,'Linewidth',1);
hold off
legend({'Risk Reduction','Remaining Risk'},'EdgeColor','none')
% set(gca, 'xlim', [1 1000]);
set(gcf,'color','w');
ax = gca;
ax.YAxis.Exponent = -3;
xticks([1 10 100 1000]);
xticklabels(xticks);
Figure 3:
To achieve the desired plot, you can combine the solutions provided for the above two figures. Here's how you can do it:
figure(3);
semilogx(x,y2,'Marker',".",'Linewidth',1,'color',"r");
hold on
semilogx(x,y3,'Linewidth',1);
hold off
legend({'Risk Reduction','Residual Risk'},'EdgeColor','none')
set(gcf,'color','w');
ax = gca;
ax.YAxis.Exponent = -3;
xticklabels(arrayfun(@changeFig1XLabel, xticks, 'UniformOutput', false));
function n = changeFig1XLabel(label)
if label < 1e-04
n = sprintf('%.0e', label);
else
n = sprintf('%.4g', label);
end
end
With these changes you can achieve your desired output.
I hope this helps.
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!