matlabmatlab-figurepareto-chart

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


I am trying to bold the right side y axis for a Pareto plot in Matlab, but I can not get it to work. Does anyone have any suggestions? When I try to change the second dimension of ax, I get an error: "Index exceeds matrix dimensions.

Error in pcaCluster (line 66) set(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)

Solution

  • Actually you need to add an output argument during the call to pareto and you will then get 2 handles (the line and the bar series) as well as 2 axes. You want to get the YTickLabel property of the 2nd axes obtained. So I suspect that in your call to pareto above you do not need to supply the ax argument.

    Example:

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

    Now if you use this command:

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

    you get the following (or something similar):

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

    What you can do is actually to erase them altogether and replace them with text annotations, which you can customize as you like. See here for a nice demonstration.

    Applied to your problem (with dummy values from the function docs), here is what you can do:

    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)
    

    which gives this:

    enter image description here

    You can of course customize everything you want like this.