Plot Solid Positive-Valued/Dashed Negative-Valued Contour Plot
显示 更早的评论
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.

采纳的回答
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 个评论
Offline for an hour because some idiot ISP technician disconnected the entire neighbourhood!
Try this:
xy = readmatrix('xy.txt');
Z = readmatrix('Z.txt');
[xl,xh] = bounds(xy(:,1));
[yl,yh] = bounds(xy(:,2));
[zl,zh] = bounds(Z);
xv = linspace(xl, xh, 150);
yv = linspace(yl, yh, 150);
[Xm,Ym] = ndgrid(xv, yv);
Zm = griddata(xy(:,1),xy(:,2),Z, Xm, Ym, 'natural');
figure
hold on
[CM,cc] = contour(Xm,Ym,Zm,'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
grid
axis('equal')
producing:

.
Thats fantastic. Thanks a lottttt.
As always, my pleasure!
And thanks for an interesting problem!
I know it's been quite a while since you posed this solution but it works perfectly until I try to set the contour levels manually.
For example if I change the cc.LevelList or cc.LevelStep, it does not show anything. Of course I can see everything with solid lines if I set the color to 'k', but how can I have dashed negatives while controlling the contour levels?
@doruk isik — I can’t determine what the problem is with your implementation.
Another approach could be —
[X,Y,Z] = peaks(50);
Zext = [min(Z(:)) max(Z(:))];
Zneg = linspace(Zext(1), -0.5, 7);
Zpos = linspace(0.5, Zext(2), 7);
lblfmt = '%.2f';
figure
contour(X, Y,Z, Zneg, '--', 'ShowText',1, 'LabelFormat',lblfmt, 'DisplayName','Negative Contours')
hold on
contour(X, Y, Z, Zpos, '-', 'ShowText',1, 'LabelFormat',lblfmt, 'DisplayName','Positive Contours')
contour(X, Y, Z, [0 0], ':k', 'ShowText',1, 'LabelFormat',lblfmt, 'DisplayName','Zero Contours')
hold off
colormap(turbo(14))
legend('Location','northoutside','Orientation','horiz')

It depends on what you want to do and how you want to do it.
.
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

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.
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Contour Plots 的更多信息
标签
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
