2

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.

Sardar Usama
  • 19,536
  • 9
  • 36
  • 58
Stephen Burns
  • 162
  • 2
  • 17

3 Answers3

4

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')
Sardar Usama
  • 19,536
  • 9
  • 36
  • 58
0

Have you tried to use the set command. For example:

%create some data
x=1:10;
y=rand(1,10);
%bar plot
bar(x,y)
%shift the x to the left 0.4
set(gca, 'XTick', x +.4)

I tried to google your problem and found this answer directly here: XTick labels and Stacking in bar plot

Irreducible
  • 864
  • 11
  • 24
  • You require `-0.4` offset here for the default value of `'BarWidth'`. In general, you need to know the `'BarWidth'` as well and set that offset accordingly. – Sardar Usama Aug 28 '17 at 11:12
  • You are completely right, I thought I leave this part to be figured out by himself. Great answer from you – Irreducible Aug 28 '17 at 13:21
0

Is there a reason why you aren't just using hist or the newer histogram? Investigating the source code for hist using edit hist I find that they are simply using bar with the 'hist' option. Something like this:

edges = 1:10
binwidth = 1
x = edges + binwidth/2
nn = 21:30

bar(x, nn, [min(edges), max(edges)], 'hist')

Gives

enter image description here

You can check the code for histogram as well to see how they are doing it there, but it's a little move involved.

chthonicdaemon
  • 19,180
  • 2
  • 52
  • 66
  • OK, I'll add a reference to `histogram`. – chthonicdaemon Aug 28 '17 at 05:56
  • `minmax` requires Neural Networks Toolbox. Use simple `min` and `max` instead – Sardar Usama Aug 28 '17 at 10:41
  • I was trying to use histogram when I was initially trying to do this, but there was always lots of whitespace between bars and I couldn't figure out how to change bounds, and the mathworks tutorials weren't helping me any, so I decided to just use this to simplify it – Stephen Burns Aug 28 '17 at 18:49
  • @chthonicdaemon can you explain to me what each variable does? I'm trying to set that up with the xTick using 20.0:.5:24.0, and using the matrix of 8 values that I provided in the question. However, I'm trying to edit the variables from the example that you gave, but I can't get it to print as I like. I think nn is the matrix of values that I'm trying to plot. Changing edges to 20.0:.5:24.0 doesn't print the graph I'm expecting though. – Stephen Burns Aug 28 '17 at 19:13
  • @StephenBurns I think the `histc` option @SardarUsama has in his answer is probably what you want. This business of calculating the offset and using hist seems like a waste if you have that option. I'm guessing `hist` was written before `bar` had the `histc` option. – chthonicdaemon Aug 29 '17 at 04:21