Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
840 views
in Technique[技术] by (71.8m points)

plot - How to superimpose two contour maps onto each other in matlab?

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]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The end result you seek is possible using contourc or using contour specifying the same contours (isolines).

This answer extends this answer in Approach 1 using contourc and provides a simple solution with contour in Approach 2.

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.

Approach 1

Example Data:

% 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

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.

Two contour plots

We can overlay the isolines since they share the same (x,y) domain. For example, they both equal 0.8 as displayed below.

Overlay of isolines for Z, W, for value of 0.8

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')

Overlay Multiple Isolines from 2 Contour Plots
We can also do this for more isolines at a time.
Overlay of multiple isolines

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')

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)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...