3D plot seems to be swapping my x and y axis labels! Suggestions please?

12 views (last 30 days)
Hi there,
It seems like I must have done something bizarre, but I would swear that implementing hist3 and interp2 together results in x and y axis labels that do not match the distributions plotted.
My X variable is clustered around 1, while Y is distributed around 0, but the 3D plot seems to swap the Xtick and Ytick labels. I think I've seen this before using hist3 alone, but with the extra complication of meshgrid, etc. involved here, I'm not sure how to fix it (or if I'm overlooking a reason not to fix it).
I'm attaching two plots--the output from scatterhist showing the appropriate distribution as well as the 3D problem plot and my code is below.
Figure 1 shows the relevant distributions:
Figure 2 with (possible?) axis issue:
Here's the code that produces the issue:
figure(1); clf;
scatterhist(A,B,'Nbins',[20,20])
xlabel('Correct X'); ylabel('Correct Y')
figure(2); clf;
[N, xout]=hist3(transpose([A;B]),[10,10]);
[Xgrid,Ygrid] = meshgrid(min(cell2mat(xout(1))):range(cell2mat(xout(1)))/99:max(cell2mat(xout(1))), min(cell2mat(xout(2))):range(cell2mat(xout(2)))/99:max(cell2mat(xout(2))));
Vq = interp2(cell2mat(xout(1)),cell2mat(xout(2)),N,Xgrid,Ygrid,'spline');
surf(Xgrid,Ygrid,Vq./164);
xlabel('X label: this distribution does not match my data!')
ylabel('Y label: this distribution does not match my data!')
Thanks for any insight you can offer!
Andrea
  2 Comments
Geoff Hayes
Geoff Hayes on 17 Jul 2014
If you were to just run the line
hist3(transpose([A;B]),[10,10]);
what does the histogram look like? If after, you run
[N, xout]=hist3(transpose([A;B]),[10,10]);
do N and xout make sense given that histogram? As xout is used for meshgrid, what are its elements?
Andrea
Andrea on 17 Jul 2014
Interesting. The hist3 output does make sense--axis labels match the distributions it plots. Thanks for the suggestion. This gives me confidence that I am totaling and plotting the correct values. The xout variable is a 1x2 cell array in which the first cell contains the histogram bins for my A variable and the second cell contains the histogram bins for my B variable as expected. Looking at the distribution across N is helpful though. If interp2 treats the N rows as X (in taking the meshgrid output), that could produce this error!
Yup. Okay, thanks!
So, the message is: don't trust that the original X and Y inputs to hist3 will be preserved across these 3D manipulations.

Sign in to comment.

Accepted Answer

Andrea
Andrea on 17 Jul 2014
You're so right! I also discovered I can do the same thing by feeding in the N' to the interp2 function, too. Thanks for your response.
Andrea
  2 Comments
Kelly Kearney
Kelly Kearney on 17 Jul 2014
Yes, hist3 and interp2 use opposite row/column conventions, which is where the error was coming from. You can also clean up some of your syntax by eliminating the cell2mat calls and just accessing the cell contents:
figure(1); clf;
scatterhist(A,B,'Nbins',[20,20])
xlabel('Correct X'); ylabel('Correct Y')
figure(2); clf;
[N, xout]=hist3(transpose([A;B]),[10,10]);
xg = linspace(min(xout{1}), max(xout{1}), 100);
yg = linspace(min(xout{2}), max(xout{2}), 100);
[Xgrid, Ygrid] = meshgrid(xg,yg);
Vq = interp2(xout{1}, xout{2}, N', Xgrid, Ygrid, 'spline');
surf(Xgrid,Ygrid,Vq./164);

Sign in to comment.

More Answers (1)

Star Strider
Star Strider on 17 Jul 2014
I didn’t completely troubleshoot your code, but there may be somthing amiss with the hist3 arguments.
In any event, if you insert this line:
xout = fliplr(xout);
right after the hist3 call, everything works!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!