This short video makes a 2d histogram as an alternative to plotting data points and visually estimating where the most data is.
By
Doug Hull
Doug first used MATLAB in 1994, could not figure it out until he got some help in 1995. He is now dedicated to making sure that no one else wastes a year of their life not knowing MATLAB like he did.
Doug, I am a little confused…I assume x and y are just random coordinates…you then created xd, yd…what is this actually?? How did you do it? Then in linespace you use x,y again, but in interp you use xd,yd. I think I understand the concept, but I’m lost in the details. Can you post all the code so I can run what you did??
XD and YD are the random points. When used in the interp1 function, XD and YD are the raw data that is then interpolated to the nearest nicely spaced data points in XI.
Sorry, I’m lost. I could not follow what’s happening without the data set to reproduce the example. Fortunately for me, the hist3 function seems to do all the heavy lifting I need.
randx = randn(100000,1);
randy = randn(100000,1);
z = hist3([randx randy],[40 40]);
surf(z)
You needed to transpose xr and yr so they are columns when you put them together. The way it was done made them a long row, not two columns. Works fine now!
Without more context, I can not really answer the question. Please contact http://www.mathworks.com/support with all the relevant files to reproduce, and we will take a look at it.
I would put a break point on the line with accumarray. Once stopped in the debugger, figure out what the inputs to accumarray are. It looks like the first one is not what is expected.
Thanks for the tutorial. Nice thing, I never came up against accumarray which seems to be very useful!
But actually there is a mistake when calculating the BIN-number: since interp1 assigns the nearest value in every dimension, all data points say in x-direction belonging to BIN 1 and half of them in BIN 2 will be assigned to (1,:). Correct should be interpolating to
xr = interp1(xi,0.5:numel(xi)-0.5,xd,'nearest')';
yr = interp1(yi,0.5:numel(yi)-0.5,yd,'nearest')';
In the example code, the data from xd will be plotted on the Y axis, and yd on the X axis. To get a properly labeled graph, with the X data along the X axis, you actually need to say:
surf(xi, yi, Z');
Why is that? In the example, we have
Z = accumarray([xr yr], 1, [n n]);
For instance, if something falls in the 1st X-bin and the 5th Y-bin, it will get counted in Z(1,5). But, the MATLAB plotting commands like SURF and CONTOUR take their inputs in the opposite way: To have something show up at (1,5) in the graph, it should be in Z(5,1).
Hope this is useful to the next person who’s thinking sideways!
-Thomas
Thanks for the video! I was able to use it effectively for 2 vectors (xd & yd).
I had some other questions about the extent to which I could use this code, or if there are other options available.
1. Is there an alternative to accumarray that will take negative inputs -or am I able to edit accumarray in a way that would allow it to take negative inputs?
2. Is there any way to have such a histogram plot for 3 vectors (xd, yd and zd)? What I would like to have is the x and y coordinates with z as the height and color coded by the frequency distribution. I am basically trying to visualize a special euclidean(2) space (since my z vector lies on polar coordinates). Is there any way to adapt your code to work for 3 vectors in this way?
I know these questions don’t directly apply to the working of the posted tutorial, but any suggestions would be very much appreciated!
Thanks in advance!
I believe I have implemented the 3d analogy for use with a 3d color histogram. Correct me if this is wrong but it seems to be working. Hope this helps someone.
Hi there, I would like to ask if you know how to label the peak values of the data points in your 3D plot. I can’t seem to find any commands in MATLAB that are able to perform that function. Thank you.
Good tutorial. This is one type of problem I often find myself trying to solve.
In fact, I find I try to solve it so often that I took the time to write a properly optimized and documented function for the file exchange:
http://www.mathworks.com/matlabcentral/fileexchange/23238-cloudplot
Doug, I am a little confused…I assume x and y are just random coordinates…you then created xd, yd…what is this actually?? How did you do it? Then in linespace you use x,y again, but in interp you use xd,yd. I think I understand the concept, but I’m lost in the details. Can you post all the code so I can run what you did??
@John,
XD and YD are the random points. When used in the interp1 function, XD and YD are the raw data that is then interpolated to the nearest nicely spaced data points in XI.
Make sense?
Doug
Doug, in the linspace functions then you should use xd,yd not x,y…was that just a typo?? If so all makes sense, thx
@John,
I can see where that would work too. For simplicity, I had hidden where the data came from. X and XD both had the same range, so it did not matter.
I had originally planned on showing where the data came, but in production the movie got too long, so it ended on the cutting room floor!
Good catch!
Doug
Sorry, I’m lost. I could not follow what’s happening without the data set to reproduce the example. Fortunately for me, the hist3 function seems to do all the heavy lifting I need.
Doug,
This is a great code! I am also lost though. When I run the line
I get the following error:
Also, can you comment how this differs from hist3.m?
@Doug
Please post all the code so I can reproduce it by copying and pasting.
Doug
@dhull
Here is what I was doing to try and duplicate your code:
I copied everything save the declarations of xd, yd out of your example in the video. Thanks!
@Douglas,
z = accumarray([xr' yr'], 1, [n n]);
You needed to transpose xr and yr so they are columns when you put them together. The way it was done made them a long row, not two columns. Works fine now!
Doug
@dhull
Thanks! Works great now — very nice code!
I received this error:
Error in ==> testrun at 22
z = accumarray([xr' yr'], 1,[n n]);
Is there anyway to get around this or what is the best way to figure out how to maximize the amount of data without excedding the memory limits?
Thanks,
Doug (Ohio State University)
@Doug,
Without more context, I can not really answer the question. Please contact http://www.mathworks.com/support with all the relevant files to reproduce, and we will take a look at it.
Thanks,
doug
I’m trying to use this function (which I’ve modified below) but I’m getting an error
EngSpd & EngTorq are independant 24000 x 1 vectors
SpdBP is a 1×16 vector
TrqBP is a 1×21 vector
Any ideas on how to fix this issue?
Thanks
Colin
@Colin,
I would put a break point on the line with accumarray. Once stopped in the debugger, figure out what the inputs to accumarray are. It looks like the first one is not what is expected.
Doug
Thanks for the tutorial. Nice thing, I never came up against accumarray which seems to be very useful!
But actually there is a mistake when calculating the BIN-number: since interp1 assigns the nearest value in every dimension, all data points say in x-direction belonging to BIN 1 and half of them in BIN 2 will be assigned to (1,:). Correct should be interpolating to
which requires
Best
Andrea
@Andrea,
Thank you for catching a subtlety there!
Doug
In the example code, the data from xd will be plotted on the Y axis, and yd on the X axis. To get a properly labeled graph, with the X data along the X axis, you actually need to say:
Why is that? In the example, we have
For instance, if something falls in the 1st X-bin and the 5th Y-bin, it will get counted in Z(1,5). But, the MATLAB plotting commands like SURF and CONTOUR take their inputs in the opposite way: To have something show up at (1,5) in the graph, it should be in Z(5,1).
Hope this is useful to the next person who’s thinking sideways!
-Thomas
I made the changes suggested by Andrea and myself, and put them into a nice copy-pasteable (and editable!) file on GitHub:
https://gist.github.com/883933
I am trying to plot out a point spread function, there are positive and negative x and y values and I get this message
??? Error using ==> accumarray
First input SUBS must contain positive integer
subscripts.
any help?
@Jbrand,
You can not pass negative numbers to accumarray. Check your inputs.
Doug
Thanks for the video! I was able to use it effectively for 2 vectors (xd & yd).
I had some other questions about the extent to which I could use this code, or if there are other options available.
1. Is there an alternative to accumarray that will take negative inputs -or am I able to edit accumarray in a way that would allow it to take negative inputs?
2. Is there any way to have such a histogram plot for 3 vectors (xd, yd and zd)? What I would like to have is the x and y coordinates with z as the height and color coded by the frequency distribution. I am basically trying to visualize a special euclidean(2) space (since my z vector lies on polar coordinates). Is there any way to adapt your code to work for 3 vectors in this way?
I know these questions don’t directly apply to the working of the posted tutorial, but any suggestions would be very much appreciated!
Thanks in advance!
I believe I have implemented the 3d analogy for use with a 3d color histogram. Correct me if this is wrong but it seems to be working. Hope this helps someone.
colorimage = imread(‘rgb.jpg’);
nbins = 16;
redmat = colorimage(:,:,1);
greenmat = colorimage(:,:,2);
bluemat = colorimage(:,:,3);
ri = linspace(0,255,nbins);
gi = linspace(0,255,nbins);
bi = linspace(0,255,nbins);
rtp = interp1(ri,1:numel(ri),double(redmat(:)),’nearest’);
gtp = interp1(gi,1:numel(gi),double(greenmat(:)),’nearest’);
btp = interp1(bi,1:numel(bi),double(bluemat(:)),’nearest’);
Z=accumarray([rtp,gtp,btp],1,[nbins,nbins,nbins]);
The only issue I have found is displaying this behemoth, since surf() only takes in mxn or mxnx3.
Hi there, I would like to ask if you know how to label the peak values of the data points in your 3D plot. I can’t seem to find any commands in MATLAB that are able to perform that function. Thank you.
Check out the command >>annotation