Wrong patch using the Faces and Vertices properties when projected on map (patchm)

2 views (last 30 days)
I am trying to color lines by a specific value using patch and patchm where I use vertices and faces (my faces are actually only lines with a start and end point). The lines connect some station pairs, i.e. station 1 with station 2, station 1 with station 3, etc. Each station pair is defined by latitude and longitude and stored in the variable verts.
verts(1:10,:)
ans =
63.4946 9.7362
62.7815 7.1518
63.4946 9.7362
62.7836 8.8780
63.4946 9.7362
62.7205 10.0433
63.4946 9.7362
62.5622 11.5530
63.4946 9.7362
62.1345 5.9689
The specific values for the station pairs are stored in the variable cohmap.
cohmap(1:10)
ans =
0.9318
0.9318
1.0166
1.0166
1.0346
1.0346
1.1186
1.1186
1.0251
1.0251
When I use a simple x,y plot using patch everything works out correctly. The correct station pairs are connected and colored by their correct values.
fac=[1:2:10; 2:2:10]'; p=patch('Faces',fac,'Vertices',verts,'FaceColor','none','EdgeColor','flat', 'LineWidth',1);
set(gca,'CLim',[0 1]);
colormap(jet);
set(p,'FaceColor','flat','FaceVertexCData',cohmap,'CDataMapping','scaled')
colorbar
for snm = 1:length(stlas)
plot(stlos(snm), stlas(snm), 'k^', 'MarkerFaceColor', 'k');
end
However, when I try to project the patch to a map using patchm I get this result:
ax = worldmap([57 64], [4 14]);
set(ax, 'Visible', 'on', 'FontSize',8)
setm(gca,'FLineWidth',1, 'fontsize', 4, 'LabelFormat', 'none', 'MLineLocation', 3, 'PLineLocation', 3, 'MLabelLocation', [6 9 12 15], 'PLabelLocation', [57 60 63], 'MLabelRound', 0, 'PLabelRound', 0);
p = patchm(verts(:, 1), verts(:, 2), 'Faces', fac, 'FaceColor','none','EdgeColor','flat', 'LineWidth',2, 'FaceVertexCData',cohmap,'CDataMapping','scaled');
set(gca,'CLim',[0 1]);
colormap(jet);
colorbar
for snm = 1:length(stlas)
plotm(stlas(snm), stlos(snm), 'k^', 'MarkerFaceColor', 'k');
end
borderN = shaperead('/path/NOR_adm0.shp', 'UseGeoCoords', true);
borderS = shaperead('/path/SWE_adm0.shp', 'UseGeoCoords', true);
geoshow(borderN.Lat, borderN.Lon, 'Color', [0.66, 0.66, 0.66],'linewidth', 0.5)
geoshow(borderS.Lat, borderS.Lon, 'Color', [0.66, 0.66, 0.66],'linewidth', 0.5)
Obviously, using patchm wrong station pairs are used. What is going wrong here? Why I get this using patchm?

Accepted Answer

Kelly Kearney
Kelly Kearney on 31 Mar 2015
Last I checked, patchm doesn't accept face/vertex input data... (which is a pain in the ass, because plotting triangulated patches is often the only way to properly render complex polygons with holes (like, say, land).
As a workaround, I do the face/vertex expansion manaually. This will give you the proper connections:
verts = [...
63.4946 9.7362
62.7815 7.1518
63.4946 9.7362
62.7836 8.8780
63.4946 9.7362
62.7205 10.0433
63.4946 9.7362
62.5622 11.5530
63.4946 9.7362
62.1345 5.9689];
cohmap = [...
0.9318
0.9318
1.0166
1.0166
1.0346
1.0346
1.1186
1.1186
1.0251
1.0251];
fac = [1:2:10; 2:2:10]';
vlon = verts(:,2);
vlat = verts(:,1);
lat = vlat(fac);
lon = vlon(fac);
c = cohmap(fac);
ax = worldmap([57 64], [4 14]);
h = patchm(lat', lon', 'r');
But... not the right color, since I've never been able to get mapped patches to interpret edge color correctly. For that, I find you need to plot each line/patch independently, and explicitly set the edge color to an RGB value:
for ii = 1:size(lon,1)
h(ii) = patchm(lat(ii,:), lon(ii,:), 'k');
end
clims = [min(cohmap) max(cohmap)];
col = interpcolor(cohmap, jet(), [0 1]);
set(h, {'edgecolor'}, num2cell(col(1:5,:),2));
(Find interpcolor here)

More Answers (1)

Chad Greene
Chad Greene on 31 Mar 2015
I've had similar problems with patchm before. I'm not sure why it helped in my particular case, but the fix I found was to flip the arrays of lats and lons, like this:
p = patchm(flipud(verts(:, 1)),flipud(verts(:, 2)),...
No guarantee that it'll work, but give it might be worth a shot.
  1 Comment
Colibri
Colibri on 1 Apr 2015
Unfortunately, that did not work for me. Would have been too good to be true ... Maybe it depends on the matlab version you are using? I use the 2012a one.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!