Sanka
Sanka

Reputation: 177

Overlay the data points which make-up a contour plot matrix on the same plot in MATLAB

Hope the title gave an adequate description of my problem. Basically, I am generating a contour plot in MATLAB using the contourf (x,y,z) function, where x and y are vectors of different lengths and z is a matrix of data with dimensions of x times y. The contourf plot is fine, however, I am looking to overlay this plot with the actual data points from the matrix z. I have tried using the scatter function, but I am getting an error message informing me that X and Y must be vectors of the same length - which they're not. Is there any other way to achieve this?

Thanks in advance for any help/suggestions!

Upvotes: 2

Views: 643

Answers (1)

Geoff
Geoff

Reputation: 1222

I think meshgrid should help you.

z = peaks;               %// example 49x49 z data
x = 1:20;
y = 1:49;
z = z(y,x);              %// make dimensions not equal so length(x)~=length(y)
[c,h] = contourf(x,y,z); 
clabel(c,h); colorbar; 

[xx,yy]=meshgrid(x,y);   %// this is what you need
hold on;
plot(xx,yy,'k.');        %// overlay points on contourf

Notice plot suffices instead of scatter. If you insist, scatter(xx(:),yy(:),10), for example, does the trick. Although my example isn't particularly interesting, this should hopefully get you started toward whatever you're going for aesthetically.

dots over contour

Upvotes: 1

Related Questions