2

I want to use MATLAB histogram to bin the largest values in my dataset into one bin so they are visible on the y axis, there are very few compared to the smaller numbers. I can use BinEdges to group the numbers greater than 4.4 into one bin, but, I would like the bin in the figure to be visually the same width as the rest of the bins. That is, shrink the scale on the X axis but only for the part between 4.4 and 10. I can't log scale the data. Is there a way to scale the axis, or break it between 4.4 and 10?

Myhistogram=histogram(mydata, 'FaceColor', 'r', 'Normalization', 'probability', 'BinEdges', [0 0.1:0.05:4.4 10]);

histogram

user1490248
  • 139
  • 2
  • 6
  • A not so elegant way would be to simply smack all values higher than 4.4 to 4.4: `Data(Data>4.4) = 4.4` or something. – Adriaan Apr 24 '17 at 22:10

1 Answers1

3

There are two ways to achieve this:

  1. The super-easy way: If you just want to bin everything from 4.4 and higher (even past 10), then just put Inf instead of 10 in the last bin edge:

    Myhistogram = histogram(mydata, 'FaceColor', 'r', ...
                                    'Normalization', 'probability', ...
                                    'BinEdges', [0 0.1:0.05:4.4 Inf]);
    

    and here's what I got (using mydata = 1+1.5.*randn(1,10000);):

    enter image description here

    Note that this simply cuts the x-axis off at a nice point! If you extend the x-axis limit, you will see a last bin like in your example that just keeps going. If you actually want a finite last bin, then...


  1. Bin first, then plot: You can first do your binning using histcounts, then display the results with histogram and an altered set of edges:

    [N, edges] = histcounts(mydata, 'Normalization', 'probability', ...
                                    'BinEdges', [0 0.1:0.05:4.4 10]);
    histogram('BinEdges', [edges(1:end-1) 4.45], 'BinCounts', N, 'FaceColor', 'r');
    

    Notice I removed the last element of edges and added a value of 4.45 to make the last bin the same width as the previous ones. And here's the histogram (with the same data from option 1):

    enter image description here

gnovice
  • 125,304
  • 15
  • 256
  • 359