Problem with scatter plot over pcolor

5 views (last 30 days)
Hello, I'm trying to plot gridded ocean model data and then overlay a scatter plot of irregular points (observation stations). For some reason I get errors and no plots at all whenever I try the code below, but of course the pcolor on its own works fine.
subplot(2, 1, 1)
pcolor(grid.lon, grid.lat, squeeze(data(i, :, :)))
hold on
scatter(points(:,1), points(:,2))
And this is the error I get (the number of which is equal to the number of points I am trying to scatter):
Warning: Patch FaceVertexCData length (0) must equal Vertices length (1) for flat EdgeColor
Warning: Patch FaceVertexCData length (0) must equal Vertices length (1) for flat EdgeColor
Warning: Patch FaceVertexCData length (0) must equal Vertices length (1) for flat EdgeColor
Warning: Patch FaceVertexCData length (0) must equal Vertices length (1) for flat EdgeColor
Warning: Patch FaceVertexCData length (0) must equal Vertices length (1) for flat EdgeColor
Warning: Patch FaceVertexCData length (0) must equal Vertices length (1) for flat EdgeColor
Warning: Patch FaceVertexCData length (0) must equal Vertices length (1) for flat EdgeColor
Warning: Patch FaceVertexCData length (0) must equal Vertices length (1) for flat EdgeColor
Warning: Patch FaceVertexCData length (0) must equal Vertices length (1) for flat EdgeColor
Warning: Patch FaceVertexCData length (0) must equal Vertices length (1) for flat EdgeColor
Warning: Patch FaceVertexCData length (0) must equal Vertices length (1) for flat EdgeColor
Warning: Patch FaceVertexCData length (0) must equal Vertices length (1) for flat EdgeColor
Warning: Patch FaceVertexCData length (0) must equal Vertices length (1) for flat EdgeColor
Warning: Patch FaceVertexCData length (0) must equal Vertices length (1) for flat EdgeColor
Warning: Patch FaceVertexCData length (0) must equal Vertices length (1) for flat EdgeColor
Warning: Patch FaceVertexCData length (0) must equal Vertices length (1) for flat EdgeColor
Warning: Patch FaceVertexCData length (0) must equal Vertices length (1) for flat EdgeColor
Warning: Patch FaceVertexCData length (0) must equal Vertices length (1) for flat EdgeColor
Warning: Patch FaceVertexCData length (0) must equal Vertices length (1) for flat EdgeColor
Warning: Patch FaceVertexCData length (0) must equal Vertices length (1) for flat EdgeColor
Warning: Patch FaceVertexCData length (0) must equal Vertices length (1) for flat EdgeColor
Warning: Patch FaceVertexCData length (0) must equal Vertices length (1) for flat EdgeColor
Warning: Patch FaceVertexCData length (0) must equal Vertices length (1) for flat EdgeColor
Warning: Patch FaceVertexCData length (0) must equal Vertices length (1) for flat EdgeColor
Warning: Patch FaceVertexCData length (0) must equal Vertices length (1) for flat EdgeColor
Warning: Patch FaceVertexCData length (0) must equal Vertices length (1) for flat EdgeColor
Warning: Patch FaceVertexCData length (0) must equal Vertices length (1) for flat EdgeColor
Warning: Patch FaceVertexCData length (0) must equal Vertices length (1) for flat EdgeColor
Warning: Patch FaceVertexCData length (0) must equal Vertices length (1) for flat EdgeColor
Warning: Patch FaceVertexCData length (0) must equal Vertices length (1) for flat EdgeColor
Warning: Patch FaceVertexCData length (0) must equal Vertices length (1) for flat EdgeColor
Warning: Patch FaceVertexCData length (0) must equal Vertices length (1) for flat EdgeColor
There must be some kind of work around to make this work.

Accepted Answer

Alexander
Alexander on 22 Apr 2011
Actually, It turns out that it was a problem with when I was calling the 'shading' handle. It needed to be before the scatter plot.

More Answers (2)

Jim Hokanson
Jim Hokanson on 21 Jun 2013
Edited: Jim Hokanson on 21 Jun 2013
Actually I think this is a bug. I am by no means a patch expert but it appears that scatter is implemented as a hggroup with a set of child patches that are the individual scatter points. What's really confusing to me is that Matlab seems to use two methods of coloring patches, one based on x,y,z data points and cData, and another based on vertices, faces, and FaceVertexCData. When switching from faceted to flat shading, it appears that Matlab switches to using vertex information (from xyz data), which given the description of the shading flat algorithm makes sense.
Now for the bug:
Scatter can be called with a [1 x 3], a [1 x m], a [m x 1], or a [m x 3] matrix (or a color string), oops, and the default which is also a single color (your case). If you use a single color, then then the hggroup has sufficient CData (I'm guessing) that Matlab doesn't bother to set CData or FaceVertexCData although the vertices are set. When you set shading to flat, then you have a non-empty vertices property and an empty FaceVertexCData property and then no one is happy.
In general the following code will reproduce this result:
N = 10 %For fun times make N even larger!
scatter(1:N,1:N);
shading flat
  3 Comments
Jim Hokanson
Jim Hokanson on 21 Jun 2013
Ha! Yeah, I am in the middle of submitting a bug report on the topic. The first reply wasn't that helpful (actually just flat out wrong). I just get a kick out of your reply because I am also in the process of switching over to Python. The process has not been nearly as smooth as I had hoped (in my week or so of doing this) but I think it will get better.
Kelly Kearney
Kelly Kearney on 21 Jun 2013
While I don't have an easy-to-replicate example at my figertips right now, I'll add that I've seen something similar to this when using the patchm function. Supposedly, patchm should allow the same cdata input as the patch function, but I've found it often fails to interpret scaled input properly; instead I have to manually set the ('facecolor', 'flat', 'cdata', cval) after the patch has been created. I haven't looked too closely at the cause, but it may have something to do with the fact that map axis surface objects assume shading flat by default.

Sign in to comment.


Walter Roberson
Walter Roberson on 22 Apr 2011
One of your variables is empty, it appears.

Community Treasure Hunt

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

Start Hunting!