Contour and Scatter in the same figure

74 views (last 30 days)
Hello all,
I have a problem using the functions "contour" and "scatter3". I hope I can explain it clearly: I have 2 triplets of data: x,y,w and x,y,z. The x and y coordinates are the same for both of them.
I know how to represent a triplet using the function "scatter3". I also know how to make a contour plot of these data (separetely for the w and z coordinates) using the meshgrid and griddata functions.
The problem I have happens when I want to make a contour plot (with the values of w) and a scatter3 plot(with the z values) in the same figure. Actually, I can represent both plots simultaneously. The contour plot is normal, but the scatter3 plot has all the points with the same color, and I wanted it to have different colour depending on the values of its z variable (as usual). This way I could see easier (with the help of the different colours) the values of both variables in one single plot.
Does anyone know how to do it??
Thanks in advance!
P.S. I have tried this in Matlab 7.3 and 7.6 and it does not work in any of them.

Accepted Answer

Walter Roberson
Walter Roberson on 16 May 2011
contour internally uses integer CData values if colors are not specified, and normally the integers get scaled to the total range of the color map.
If scatter3 is specifically used with the raw zdata values as the colors, then when used in isolation, the raw zdata value range would get scaled to the total range of the color map.
Put these two together and unless the zdata value range happens to be from 1 to the number of contours, the color transform of one or the other is going to be messed up.
I can think of a couple of ways that one might compensate for this, but before going in to them, I would rather that xkudsraw indicate how their code is currently constructed... no point in my detailing a solution if I have guessed wrong about the problem ;-)
  2 Comments
xkudsraw
xkudsraw on 16 May 2011
I think you have guess right: I tried Patrick's example, and it worked with his data, but not with mine.
I think it is because my w values go from 70000 to 200000 (that I use for the contour plot), and the z values only from to 200...
Is this explanation enough or do you need also the exact code? Actually, its structure is almost the same as Patrick's given example.
Thanks again!
Patrick Kalita
Patrick Kalita on 16 May 2011
Thanks for the additional info. I had actually thought about address that case in my original answer, but I thought I'd keep it simple. Anyway, I've updated my answer.

Sign in to comment.

More Answers (2)

Patrick Kalita
Patrick Kalita on 16 May 2011
Are you using the optional fifth input argument of scatter3? That is the input that controls the color of the points.
Here's an example:
% Make up some data
x = rand(1,100);
y = rand(1,100);
z = x.^2 + y.^2;
w = x + y;
% Make a contour plot with the w-data.
[xg, yg] = meshgrid(0 : 0.1 : 1);
wg = griddata(x,y,w,xg,yg);
contour(xg,yg,wg);
% Make a scatter3 plot with the z-data, and also use the z-data to color the markers. The fourth input is empty ( [] ) because we want the markers to all be the same size.
hold on
scatter3(x, y, z, [], z);
% Just for good measure
colorbar
A word of caution: you may get surprising (although correct) results if w and z have vastly different ranges. When w and z have different ranges, the axes will expand its CLim property to encompass the entire range of w and z. For example, if w goes from [0 1] and z goes from [500 501], when both plots are added to the same figure, the axes' CLim will be set to [0 501]. That means the contour lines will pick colors from very very bottom of the colormap, and the scatter markers will pick up colors from the very very top of the colormap.
There are a few things you could do to change this behavior. The simplest thing is that you could scale w and/or z so that they occupy the same range.
Or you could use the axes Colormap property and a function like ind2rgb, and pass RGB colors to scatter3. That's a bit of an advanced maneuver, but then the colors of the scatter3 point markers don't rely on the axes CLim property. Here's a very simplified example to illustrate:
contour(peaks);
hold on
scatter3(1:5:25, 1:5:25, 1:5:25, [], [0 0 0; 1 0 0; 0 1 0; 0 0 1; 1 1 0], 'filled')
In reality, you wouldn't manually specify the colors like that -- you would generate that from the colormap and ind2rgb.
  3 Comments
guyklx
guyklx on 9 Feb 2020
"...you could scale w and/or z so that they occupy the same range..."
How would you do that?
Walter Roberson
Walter Roberson on 9 Feb 2020
With new enough matlab see rescale(). If your version is not new enough for that then see the poorly named mat2gray

Sign in to comment.


xkudsraw
xkudsraw on 18 May 2011
Problem solved :)I have used the function ind2rgb to obtain the new colormap for the z data. Thanks a lot you both!!

Categories

Find more on Colormaps 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!