9

I looked and couldn't find an answer for this question, so here goes.

I have some data (a 1 X 1000 vector called data) that I would like to plot the histogram information for. If I use the histogram(data) command then I get a nice enough histogram plot where the x-axis is the divided evenly into ten buckets (midpoint values of the ten equal intervals between the max and min values of the data) and the y-axis records how many occurances occured for each bucket.

What I really want is the same plot, just with the y-axis representing the bucket intervals, and the x-axis representing the count for each bucket...

That way I can stick it into a subplot next to some other information, and everything will be easier to understand (and look super cool). What's an easy way to accomplish this? Thanks!

Jimmy
  • 4,419
  • 6
  • 21
  • 30

3 Answers3

11

You can achieve what you want using the barh function. Here's an example:

testData = randn(10000,1); %# test data
[counts,bins] = hist(testData); %# get counts and bin locations
barh(bins,counts)

enter image description here

Flipping the bar chart

Here's an example showing how to flip the chart along a vertical axis.

h=barh(bins,counts); %# include previous two lines from above
set(get(h,'Parent'),'xdir','r')

enter image description here

Community
  • 1
  • 1
abcd
  • 41,765
  • 7
  • 81
  • 98
  • The call should be `barh(f,z)`, maybe you should use better variable names :) Also if you want to get the same spacing behavior as the HIST plot, add a third argument: `barh(bins,counts,'hist')` – Amro Oct 14 '11 at 23:12
  • @Amro: Cool! Thanks for the correction, I was trying it out and getting the plot of a barren christmas tree... but now it works. – Jimmy Oct 14 '11 at 23:17
  • @amro Lol, thanks! That totally was due to silly variable names. I wrote the answer without MATLAB and fired it up and included the plot within the 5 minute window. While I fixed it in my terminal, I forgot to do it here – abcd Oct 14 '11 at 23:22
  • @Jimmy Consider it an early Christmas greetings from me :D – abcd Oct 14 '11 at 23:23
  • Great, now it looks perfect. I notice that `barh` takes y values first, and x values second. I also notice there is no function `ploth` -- so I guess for a swapped plot, you can just swap the vectors, which leads me to this question: "Can you have the bars start from the right and grow out to the left?" Basically, the exact same plot, just reflected on a vertical axis? – Jimmy Oct 14 '11 at 23:28
  • @Jimmy I think they kept `barh` that way so that it's consistent with the syntax of `bar`. As for your second question, try `set(gca,'xdir','r')`. This should flip the increasing values in the `x-axis` from right to left. I can't test it now, as I don't have access to MATLAB at the moment, but I'm pretty sure it should work. It'd be great if you could confirm if this helps. – abcd Oct 14 '11 at 23:39
  • @Yoda: Yeah, so I tried the various combinations of `set(gca,['xdir'|'ydir'],['r'|'reverse'|'normal'])` and couldn't get the bar chart to budge an inch... which is a bummer, because now I really want a stalactite bar chart that grows down from the ceiling! Should I start another question and close this one? Thanks again, and happy new year! – Jimmy Oct 14 '11 at 23:51
  • @Jimmy No, I'm very certain this should work. Just give me a few minutes and I'll post an example when I get access to MATLAB. One problem could've been because the right plot was not selected and `gca` captures the wrong one. This can be offset by using axes handles. Also, please do not migrate this to chat. I hate it. – abcd Oct 14 '11 at 23:53
  • @Yoda, yeah, I tried to use your simple example and just stick that `set` statement right before the call to `barh`... I will check back here for your example in a little bit, gotta go teach right now, thanks again! – Jimmy Oct 15 '11 at 00:00
  • @Jimmy Ah, that's the mistake. The call to `set` should've been _after_ the call to `barh`. Please see my edit above. – abcd Oct 15 '11 at 00:05
5

since the HISTOGRAM function was introduced (R2014b), you can make a horizontal histogram by setting 'orientation' to 'horizontal'

example:

histogram(data,'orientation','horizontal')
petermao
  • 51
  • 1
  • 2
  • The `histogram` function also does not reorder the bars according to names (unlike `bar` or `hbar` functions). – scrutari Feb 04 '19 at 13:57
3

You can also use the regular histogram function hist and then change the point of view by typing

>> view(90, -90)
Shai
  • 111,146
  • 38
  • 238
  • 371
Erik
  • 31
  • 1