如何在Matlab中将两个轮廓图相互叠加? [英] How to superimpose two contour maps onto each other in matlab?
问题描述
我在Matlab中有两个轮廓图,并且两个图都有一条指定单个Z值的曲线.我想强加两个轮廓图,以便找到两个z值曲线相交的唯一解决方案.我该如何叠加两个等高线图?
I have two contour maps in Matlab and each of the two maps has a single curve specifying a single Z-value. I want to super impose the two contour maps so that I can find the single solution where the two z-value curves intersect. How could I go about super imposing the two contour maps?
% the two contour maps are coded the exact same way, but with different z-values
x = 0.05:0.05:1;
y = 0.0:0.05:1;
[X, Y] = meshgrid(x, y);
% Z-value data is copied from excel and pasted into an array
Z = [data]
contourf(X, Y, Z);
pcolor(X, Y, Z); hold on
shading interp
title();
xlabel();
ylabel();
colorbar
val = %z-value to plot onto colormap
tol = %tolerance
idxZval = (Z <= val+tol) & (Z >= val-tol);
plot(X(idxZval), Y(idxZval))[enter image description here][1]
推荐答案
使用 contourc
或使用
The end result you seek is possible using contourc
or using contour
specifying the same contours (isolines).
此答案使用此答案. //www.mathworks.com/help/matlab/ref/contourc.html"rel =" nofollow noreferrer> contourc
并使用
This answer extends this answer in Approach 1 using contourc
and provides a simple solution with contour
in Approach 2.
您可能会问"为什么方法2这么简单的方法1?" 如果您需要一种数值方法来搜索相交,则方法1提供了一种直接访问各个等值线的方法.
You might ask "Why Approach 1 when Approach 2 is so simple?" Approach 1 provides a way to directly access the individual isolines in the event you require a numerical approach to searching for intersections.
示例数据:
% MATLAB R2018b
x = 0:0.01:1;
y = 0:0.01:1;
[X,Y] = meshgrid(x,y);
Z = sqrt(X.^3+Y); % Placeholder 1
W = sqrt(X.*Y + X.^2 + Y.^(2/3)); % Placeholder 2
从2个轮廓图中覆盖单个等值线
模仿此答案并使用
v = [.5 0.75 .85 1]; % Values of Z to plot isolines
我们可以分别可视化这两个函数Z
和W
.
Overlay Single Isoline from 2 Contour Plots
Mimicking this answer and using
v = [.5 0.75 .85 1]; % Values of Z to plot isolines
we can visualize these two functions, Z
, and W
, respectively.
我们可以覆盖等值线,因为它们共享相同的(x,y)域.例如,它们都等于 0.8 ,如下所示.
We can overlay the isolines since they share the same (x,y) domain. For example, they both equal 0.8 as displayed below.
val = 0.8; % Isoline value to plot (for Z & W)
Ck = contourc(x,y,Z,[val val]);
Ck2 = contourc(x,y,W,[val val]);
figure, hold on, box on
plot(Ck(1,2:end),Ck(2,2:end),'k-','LineWidth',2,'DisplayName',['Z = ' num2str(val)])
plot(Ck2(1,2:end),Ck2(2,2:end),'b-','LineWidth',2,'DisplayName',['W = ' num2str(val)])
legend('show')
从2个轮廓图中覆盖多个等值线
我们也可以一次为更多等值线进行此操作.
Overlay Multiple Isolines from 2 Contour Plots
We can also do this for more isolines at a time.
v = [1 0.5]; % Isoline values to plot (for Z & W)
figure, hold on, box on
for k = 1:length(v)
Ck = contourc(x,y,Z,[v(k) v(k)]);
Ck2 = contourc(x,y,W,[v(k) v(k)]);
p(k) = plot(Ck(1,2:end),Ck(2,2:end),'k-','LineWidth',2,'DisplayName',['Z = ' num2str(v(k))]);
p2(k) = plot(Ck2(1,2:end),Ck2(2,2:end),'b-','LineWidth',2,'DisplayName',['W = ' num2str(v(k))]);
end
p(2).LineStyle = '--';
p2(2).LineStyle = '--';
legend('show')
方法2
没有让它变得漂亮...
Approach 2
Without making it pretty...
% Single Isoline
val = 1.2;
contour(X,Y,Z,val), hold on
contour(X,Y,W,val)
% Multiple Isolines
v = [.5 0.75 .85 1];
contour(X,Y,Z,v), hold on
contour(X,Y,W,v)
清除这些内容以进行演示很简单.如果val
是标量(单个数字),则c1 = contour(X,Y,Z,val);
和c2 = contour(X,Y,W,val)
允许访问每个轮廓图的等值线.
It is straightforward to clean these up for presentation. If val
is a scalar (single number), then c1 = contour(X,Y,Z,val);
and c2 = contour(X,Y,W,val)
gives access to the isoline for each contour plot.
这篇关于如何在Matlab中将两个轮廓图相互叠加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!