MATLAB: Zoomed in and inset histogram

histogramsubplotzoom

I have a histogram where I need to zoom along a specific part of the x axis (the actual probabilities in this range of the x axis is extremely low, so the bars get lost when the entire histogram is plotted) and have this subhistogram inset into the original histogram. Can someone please tell me how this can be done? Your help is much appreciated.

Best Answer

  • Sounds like you have two questions:
    First Question: How do I "zoom in" a histogram?
    The answer depends on what exactly you mean by "zoom in", but it sounds like you want to re-bin the data using a smaller range of values. There are a couple ways to do that:
    1. There is an optional second input to the histogram command in which you can specify the bin edges. You can use this to specify specific bin edges which span a smaller range than your entire data set. For example:
    x = randn(1000,1); % Data will range from roughly -3.5 to 3.5

    histogram(x, -1:.1:1); % Bin edges will only include data from -1 to 1.

    2. Option 1 hard-codes the bin edges. You can allow the bins to be determined automatically, but still reduce the range, by specifying the BinLimits:
    x = randn(1000,1); % Data will range from roughly -3.5 to 3.5
    histogram(x, 'BinLimits', [-1 1]); % Bin edges will only include data from -1 to 1.
    Second Question: How can I plot a subhistogram inset into the original histogram?
    The only way to do this is to create a second, smaller, axes that is manually placed to overlap your main axes. For example:
    x = randn(1000,1);
    mainax = axes;
    histogram(mainax, x, 'BinLimits', [-1 1]);
    ylim(mainax, [0 120]) % To make room for sub axes.
    subax = axes('Position',[0.2 0.7 0.2 0.2]);
    histogram(subax, x);