Imagesc/Contourf and plotting correctly
Show older comments
Thanks in advance for the help.
I have a series of coordinates in latitude, longitude, and depth (respectively called lat, long, and depth) and associated at each one of these points P(lat,long,depth) I have a value. The values for each of these points in contained in a 3d matrix called misfit(lat,long,depth). The idea is to make several imagesc (or contourf) plots to at varying depths to get an idea of my results and to have on each plot a point marking the minimum of the misfit (the matrix mentioned above). I contain the value and the coordinates of the minimum misfit (in that respective order) in the variable mini.
The problem is that when I do: figure; hold on imagesc(lat, long, misfit(:,:,mini(4))); plot(lat(mini(2)), long(mini(3)), 'ok', 'markersize', 15, 'markerfacecolor', 'k'); hold off
I have a surface that's minimum does not correspond at all to the point I plotted. When I go to the data cursor and go to the location of mini, it gives me back a value that is not at all the minimum value I want.
What am I missing here?
Accepted Answer
More Answers (3)
Teja Muppirala
on 4 Apr 2011
This is just a guess, but depending on how you found your minimum locations, the x and y values (mini(2) and mini(3)) might be reversed. Indexing into matrices goes vertically, then horizontally, while the PLOT function takes x (horizontal) values then y (vertical) values.
To illustrate:
M = zeros(5);
M(1,4) = -1
imagesc(M)
hold on
plot(1,4,'wx')
M(1,4) is definitely the minimum. But you can see that plot(1,4) has the coordinates in the wrong order. Reverse them and it will be ok.
plot(4,1,'wx')
2 Comments
William F.
on 5 Apr 2011
Teja Muppirala
on 6 Apr 2011
At the command line, misfit(latmin, longmin, depthmin) = minimum value = A = 0.067443 according to the 3rd line printed.
The imagesc plot with data cursor: misfit(17.89 which is very near latmin, -99.51 which is very near longmin, depthmin) = 0.06744
0.067443 is very close to 0.06744, so I'm having trouble seeing the problem. What value were you expecting to see?
Walter Roberson
on 6 Apr 2011
The coordinates that you give in the first two arguments for imagesc() are the locations of pixel centers of the bottom-right and upper-left corners. This can result in the image being larger than you expect. Still, though, that should not affect the data cursor.
In your code, you use
A = min(min(min(misfit))); i = find(misfit == A); [r,c,d] = ind2sub(size(misfit),i);
This can be better written
[A, i] = min(misfit(:)); [r, c, d] = ind2sub(size(misfit),i);
The code you have is inefficient because find can return the positions directly, [r, c, d] = find(misfit == A); However the code is also risky because there might be more than one location which exactly matches the minimum and then find would return multiple values, which you are not prepared to deal with. min() can return an array index directly and will only return one index in the form I show.
The "Index:" that shows up in your datatip is the color index for pseudo-colored images. The value it will have will depend upon the current CLim setting. Normally (e.g., with the code you seem to have used) it would correspond to the array value. In order to find out more surely, record the handle of the image that imagesc() generates and get() the CData property of that handle and examine the matrix.
Categories
Find more on Geographic Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!