0

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

EBH
  • 10,350
  • 3
  • 34
  • 59
user2100039
  • 1,280
  • 2
  • 16
  • 31
  • The x axis and y axis values or range of data are not equal. The x variable is wind speed and the y variable is temperature. Thank you, – user2100039 Aug 22 '16 at 20:44
  • An expected range for the x variable might be 0 to 30 (wind speed) and the expected y variable range for temperature to include all 54 cases might be -20 to +40. Thank you. – user2100039 Aug 22 '16 at 21:03
  • Hi - yes, that works in the same way that my current code works. The x and y axis ranges are changing with each new plot. This is not what I need. I want to compare the spread of the data with the same or fixed x and y axis range for each case above for n, where n = 1:54. Thanks! – user2100039 Aug 22 '16 at 21:42

1 Answers1

0

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

Community
  • 1
  • 1
EBH
  • 10,350
  • 3
  • 34
  • 59
  • i've tried your solution. first plot is ok. Second plot in the loop or when n == 2, I get an error on the values2 variable "Index exceeds matrix dimensions". Your thoughts? Also, the axis labels or x and y tick labels are in decimal form rather than integers. thanks, – user2100039 Aug 22 '16 at 22:20
  • I run this in a loop with no error, can you copy the line that makes the error? as for the labels, this can be edited using `sprintf` I would add it to the answer, but I'm not sure what you want to see – EBH Aug 22 '16 at 22:24
  • values2 = hist3([y(:,n) x(:,n)],[round(range(y(:,n))) round(range(x(:,n)))]) ; – user2100039 Aug 22 '16 at 22:42
  • I figured it out the error but my axis are still changing values with every n plot. I need them to be static - no changing. Thanks, – user2100039 Aug 23 '16 at 01:28
  • It is possibles, but then you may get white strips from the sided if the data in `x(:,n)` and / or `y(:,n)` has a smaller range then `y(:)` and `x(:,n)`. Keep in mind that if you want to compare all the plots, you should use the same binning, so instead of `[round(range(y(:,n))) round(range(x(:,n)))]` you would have `[round(range(y(:))) round(range(x(:)))]` for all `n`. See my edit to the answer. – EBH Aug 23 '16 at 07:44