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

17 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

Star Strider
Star Strider el 23 de Oct. de 2020
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).
  8 comentarios
doruk isik
doruk isik el 27 de Sept. de 2022
I was just testing different stuff with the code you provided in 2020. If I use it as is, it gives me the following
If I modify the contour handle cc by manually defining the levels as such
[CM,cc] = contour(Xm,Ym,Zm,'ShowText','on');
cc.LineStyle = ':';
cc.Color = 'w';
cc.LevelList=-0.1:0.02:0.1; % this is my addition
Lvls = cc.LevelList;
it gives me the following
Star Strider
Star Strider el 27 de Sept. de 2022
The contour function chooses the contour level values and number of levels itself if they aren’t specified. It likely specifies more contours than the colon operator chooses. To get more of them,, either use a smaller step size than 0.02 or use the linspace function. The colon operator fixes the step increment and lets the length (number of elements) vary. The linspace function fixes the number of elements and varies the step increment.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Contour Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by