MATLAB 如何绘制具有特定值的特殊线宽的等高线

MATLAB how to plot contour with special LineWidth for certain value

我有以下脚本:

close all; clear all; clc;

x = linspace(-2*pi,2*pi);

y = linspace(0,4*pi);

[X,Y] = meshgrid(x,y);

Z = sin(X)+cos(Y);

values = -10:0.5:10;

figure

[C,hh] = contour(X, Y, Z, values,'r', 'LineWidth',1); 

clabel(C, hh, values, 'fontsize',7)

正如你在等高线中看到的那样,所有的线都是用LineWidth = 1绘制的。我想为value = 0绘制特殊线,LineWidth = 2,如何设置?非常感谢您的帮助。

您将需要制作二次等高线图以突出显示所需的等高线水平。 MathWorks 在文档中有 an example of this

对于您的案例,我们将提供如下内容:

% Generate sample data
x = linspace(-2*pi,2*pi);
y = linspace(0,4*pi);
[X,Y] = meshgrid(x,y);
Z = sin(X)+cos(Y);
values = -10:0.5:10;

% Generate initial contour plot
figure
[C,hh] = contour(X, Y, Z, values,'r', 'LineWidth',1); 
clabel(C, hh, values, 'fontsize',7)

% Generate second contour plot with desired contour level highlighted
hold on
contour(X, Y, Z, [0 0], 'b', 'LineWidth', 2); 
hold off

其中returns以下:

并不是说我已将单个轮廓级别指定为向量。这是由 the documentation for contour:

解释的

contour(Z,v) draws a contour plot of matrix Z with contour lines at the data values specified in the monotonically increasing vector v. To display a single contour line at a particular value, define v as a two-element vector with both elements equal to the desired contour level. For example, to draw contour lines at level k, use contour(Z,[k k])

如果您想突出显示多个级别,则这不适用(例如 contour(X, Y, Z, [-1 0], 'b', 'LineWidth', 2) 突出显示 -10