Apply colormap coloring to a particular contour to indicate imaginary component
    3 views (last 30 days)
  
       Show older comments
    
I have a set of data X, Y, Z, related via a function  , where the data stored in Z are complex valued. I want to make a contour plot of this data at a precise value output value, i.e. the contour: all x and y such that
, where the data stored in Z are complex valued. I want to make a contour plot of this data at a precise value output value, i.e. the contour: all x and y such that  . The typical part of the contour plot will just be contour(X,Y,real(Z),[z z]). However, I want the actually coloring of the contour line via a colormap for the imaginary part. So, for example, if the imaginary part is more negative the line will be blue in that region whereas if the imaginary part is more postive it will be more red.
. The typical part of the contour plot will just be contour(X,Y,real(Z),[z z]). However, I want the actually coloring of the contour line via a colormap for the imaginary part. So, for example, if the imaginary part is more negative the line will be blue in that region whereas if the imaginary part is more postive it will be more red.
 , where the data stored in Z are complex valued. I want to make a contour plot of this data at a precise value output value, i.e. the contour: all x and y such that
, where the data stored in Z are complex valued. I want to make a contour plot of this data at a precise value output value, i.e. the contour: all x and y such that  . The typical part of the contour plot will just be contour(X,Y,real(Z),[z z]). However, I want the actually coloring of the contour line via a colormap for the imaginary part. So, for example, if the imaginary part is more negative the line will be blue in that region whereas if the imaginary part is more postive it will be more red.
. The typical part of the contour plot will just be contour(X,Y,real(Z),[z z]). However, I want the actually coloring of the contour line via a colormap for the imaginary part. So, for example, if the imaginary part is more negative the line will be blue in that region whereas if the imaginary part is more postive it will be more red.2 Comments
  Walter Roberson
      
      
 on 28 Feb 2024
				That would require a line that does not have constant color, since you can have sections that have the same real(Z) value but which can vary completely in imaginary component.
Accepted Answer
  DGM
      
      
 on 28 Feb 2024
        
      Edited: DGM
      
      
 on 28 Feb 2024
  
      It can be done, but not with contour() or plot().
% some fake data
x = linspace(-1,1,100);
y = x.';
z = x.*y + sqrt(x) - sqrt(y);
% get the level curves of real(z)
[cc,hh] = contour(x,y,real(z),20);
[~,allcontours] = getContourLineCoordinates(cc);
% redraw everything again 
figure
hold on;
zrange = [0 0];
for k = 1:numel(allcontours)
    % interpolate to find imag(z) along this level curve
    thisx = allcontours{k}(:,1);
    thisy = allcontours{k}(:,2);
    thisz = interp2(x,y,imag(z),thisx,thisy);
    % accumulate colorscale limits
    zrange(1) = min(zrange(1),min(imag(z(:))));
    zrange(2) = max(zrange(2),max(imag(z(:))));
    % draw the line using patch(), since line() can't do variable color
    patch([thisx;NaN],[thisy;NaN],[thisz;NaN],'EdgeColor','interp','LineWidth',1);
end
clim(zrange)
The rest will be setting up your preferred colormap and such.
This example uses Adam's contour extraction tool from the FEX (attached):
2 Comments
More Answers (0)
See Also
Categories
				Find more on Contour Plots in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




