Search code examples
matlabgraphhistogrambar-chartmatlab-figure

How to shift the XtickLabels of a bar graph to the left?


I'm currently trying to create a frequency histogram, and to do this, I had to create a bar graph that has no whitespace between the bars. However, this centers the XTickLabels in the middle of the bars. Since it's a histogram, I would like the numerical values to be at the line between each bars so that it can visually indicate intervals. Essentially, I need to shift all the tick labels to the left.
I'm using R2016a.

Looking through Mathworks only turned up answers for how to do this using line graphs, scatter plots, etc.


Solution

  • Use the Style argument of bar to set it to histc.

    v = [6 12 17 21 28 25 19 15];
    bar(v,'histc');
    

    which gives:

    out


    Furthermore, if you're interested you can change the direction of tick marks to outwards with:

    set(gca,'TickDir', 'out')