Search code examples
matlabplotbar-chartmatlab-figure

How to plot bars reversed up with negative values?


Starting by this 10x3 y matrix, where all its entries are negative values:

y = [-56   -58   -60;
     -75   -74   -72;
     -66   -66   -69;
     -67   -69   -71;
     -66   -67   -71;
     -59   -58   -57;
     -69   -71   -69;
     -59   -58   -58;
     -66   -67   -68;
     -75   -73   -72];

I need to plot it simply using bar(y), but the bars appear reversed down as shown below:

figure

How to plot this matrix in the the normal bar direction, while the y-axis ticks not change (i.e., from -95 up to -50 as in the figure) such that the bar which has -50 value is the tallest one and that has -90 is the smallest one?


Solution

  • Make the bar plot with an offset of 90 so that you plot positive values instead of negative and then adjust the ticks and tick-labels.

    bar(y+90);
    ax = gca; 
    %Limiting # of YTicks to 5 between minimum and maximum. 
    %Inputting 5 YtickLabels accordingly. Select them as per your requirement.
    ax.YTick = linspace(min(ax.YTick),max(ax.YTick),5);
    ax.YTickLabel = strsplit(num2str(-90:10:-50));  
    

    For R2014a and earlier versions, use get and set instead of dot notation to manipulate those properties.

    Output:

    out