如何在 Matlab 中更改帕累托图右轴的字体大小

How to change font size of right axis of Pareto plot in Matlab

我试图在 Matlab 中为帕累托图的右侧 y 轴加粗,但我无法让它工作。有没有人有什么建议?当我尝试更改 ax 的第二维时,出现错误: “索引超出矩阵维度。

pcaCluster 错误(第 66 行) 设置(ax(2),'Linewidth',2.0);

figure()
ax=gca();
h1=pareto(ax,explained,X);
xlabel('Principal Component','fontweight','b','fontsize',20)
ylabel('Variance Explained (%)','fontweight','b','fontsize',20)
set(ax(1),'Linewidth',2.0);
set(ax(1),'fontsize',18,'fontweight','b');
%set(ax(2),'Linewidth',2.0);
%set(ax(2),'fontsize',18,'fontweight','b');
set(h1,'LineWidth',2)

这是因为 ax 是 (first/left) 轴对象的句柄。它是一个单一的值,ax(1) 你很幸运,它又是 ax,但是 ax(2) 根本无效。

我建议阅读有关如何获取第二个轴的文档。另一个好主意始终是在绘图浏览器中打开绘图,单击您想要的任何对象以选中它,然后通过在命令 window 中键入 gco(获取当前对象)来获取其句柄。然后您可以将它与 set(gco, ...).

一起使用

实际上,您需要在调用 pareto 期间添加一个输出参数,然后您将获得 2 个手柄(直线和条形系列)以及 2 个轴。您想获得获得的第 2 个轴的 YTickLabel 属性。因此,我怀疑在上面对 pareto 的调用中,您不需要提供 ax 参数。

示例:

[handlesPareto, axesPareto] = pareto(explained,X);

现在如果你使用这个命令:

RightYLabels = get(axesPareto(2),'YTickLabel') 

你得到以下(或类似的东西):

RightYLabels = 

    '0%'
    '14%'
    '29%'
    '43%'
    '58%'
    '72%'
    '87%'
    '100%'

实际上你能做的就是将它们全部擦除,并用text注解代替,你可以根据自己的喜好自定义注解。请参阅 here 以获得很好的演示。

应用于您的问题(使用函数文档中的虚拟值),您可以执行以下操作:

clear
clc
close all

y = [90,75,30,60,5,40,40,5];
figure
[hPareto, axesPareto] = pareto(y);

%// Get the poisition of YTicks and the YTickLabels of the right y-axis.
yticks = get(axesPareto(2),'YTick')
RightYLabels = cellstr(get(axesPareto(2),'YTickLabel'))


%// You need the xlim, i.e. the x limits of the axes. YTicklabels are displayed at the end of the axis.

xl = xlim;

%// Remove current YTickLabels to replace them.
set(axesPareto(2),'YTickLabel',[])

%// Add new labels, in bold font.
for k = 1:numel(RightYLabels)    
    BoldLabels(k) = text(xl(2)+.1,yticks(k),RightYLabels(k),'FontWeight','bold','FontSize',18);
end

xlabel('Principal Component','fontweight','b','fontsize',20)
ylabel('Variance Explained (%)','fontweight','b','fontsize',20)

这给出了这个:

您当然可以像这样自定义您想要的一切。