Search code examples
matlabhistogramscalematlab-figureaxis

Matlab Hist3/Imagesc Axis Scaling


I have 2 arrays of data x and y both are 22250-by-54, and I'm trying to use hist3 and imagesc to make density plots for each x(:,n) and y(:,n) pair where n = 1:54.

I'm using imagesc(values2) where:

values2 = hist3([x(:) y(:)],[round(max(x)) round(max(y)) ]);

to use as my argument to plot for each of the 54 x and y values to get a unique axis range and it works fine. However, when I place fixed integer values for values2 such as

values2 = hist3([x(:) y(:)],[50 50 ]);

the actual values for each of the 54 columns of x and y end up getting scaled to the [50 50] parameters or if I use [100 100] and it does not reflect the ACTUAL values for each x and y. How can I fix the axis x,y ranges and keep the actual values in the fixed axis range?

I have tried also using xlim and ylim in a separate call after the call to imagesc(values2) and this does not work either - it plots my data in a very small area and leaves lots of white space around the image area.

Thank you for your help!!

I have tried the "checked" response from the link below to get to where I am now: Scatter plot with density in Matlab


Solution

  • Using the answer you referenced you can write:

    % some arbitrary data:
    x = randi(30,30,54);
    y = randi(61,30,54)-21;
    
    % constant values for every n:
    nbins = [round(range(y(:))) round(range(x(:)))]+1;
    x_lim = [min(x(:)) max(x(:))];
    y_lim = [min(y(:)) max(y(:))];
    
    % plotting:
    for n = 1:9
        [values2, centers] = hist3([x(:,n) y(:,n)],nbins);
        subplot(3,3,n) % this is just for compact demonstration
        h = imagesc(centers{:},values2.');
        title(num2str(n))
        xlim(x_lim) % keeping the x-axis limits constant
        ylim(y_lim) % keeping the y-axis limits constant
        axis xy
    end
    

    Note that you may get with areas for your plots, as in this example, if the data in x(:,n) or y(:,n) has a smaller range then y(:) and x(:,n).

    which will give: imagesc hist