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

前端 未结 1 1326
余生分开走
余生分开走 2020-12-21 23:43

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

相关标签:
1条回答
  • 2020-12-22 00:11

    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.

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

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

    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.

    0 讨论(0)
提交回复
热议问题