MATLAB: Plot Solid Positive-Valued/Dashed Negative-Valued Contour Plot

contour

How is it possible to plot a contour plot with solid lines for positive values (above zero) and dashed lines for negative values. An example plot is attached.

Best Answer

The contour function itself cannot do that, however it is srtaightforward to adapt it:
[X,Y,Z] = peaks;
figure
hold on
[CM,cc] = contour(X,Y,Z,'ShowText','on');
cc.LineStyle = ':';
cc.Color = 'w';
Lvls = cc.LevelList;
PosIdx = find(ismember(CM(1,:),Lvls(Lvls>=0)));
PosLen = CM(2,PosIdx);
NegIdx = find(ismember(CM(1,:),Lvls(Lvls<0)));
NegLen = CM(2,NegIdx);
for k = 1:numel(PosIdx)
IdxRng = PosIdx(k)+1:PosIdx(k)+PosLen(k);
plot(CM(1,IdxRng), CM(2,IdxRng), '-k')
end
for k = 1:numel(NegIdx)
IdxRng = NegIdx(k)+1:NegIdx(k)+NegLen(k);
plot(CM(1,IdxRng), CM(2,IdxRng), '--k')
end
hold off
axis('equal')
producing:
This overplots the original contours so that any text values remain visible (ini case you want them shown).