【问题标题】:How to change font size of right axis of Pareto plot in Matlab如何在Matlab中更改帕累托图右轴的字体大小
【发布时间】:2015-02-12 04:02:25
【问题描述】:

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

pcaCluster 中的错误(第 66 行) set(ax(2),'线宽',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)

【问题讨论】:

    标签: matlab matlab-figure pareto-chart


    【解决方案1】:

    实际上,您需要在调用pareto 期间添加一个输出参数,然后您将获得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)
    

    这给出了这个:

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

    【讨论】:

      【解决方案2】:

      这是因为 ax 是(第一/左)坐标区对象的句柄。这是一个单一的值,ax(1) 你很幸运,它的ax 再次,但ax(2) 根本无效。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-02-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-03
        • 1970-01-01
        相关资源
        最近更新 更多