Apply colormap coloring to a particular contour to indicate imaginary component

1 次查看(过去 30 天)
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 . 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 个评论
Walter Roberson
Walter Roberson 2024-2-28
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.
David Gillcrist
David Gillcrist 2024-2-28
Yes, that is what I'm trying to accomplish. The line color at a particular point in will depend on the imaginary value of the Z data, similar to what was done here: How to assign gradual color to a 3d line based on values of another vector.

请先登录,再进行评论。

采纳的回答

DGM
DGM 2024-2-28
编辑:DGM 2024-2-28
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 个评论
David Gillcrist
David Gillcrist 2024-2-29
This works great! I made a slight modification where I used cc = contourc(xx,yy,real(Z),[1 1]); instead of [cc,hh] = contour(x,y,real(z),20); as it allows me to get the contour info without plotting that contour plot and I'm only interested in a single contour. Additionally, immediately after
[~,allcontours] = getContourLineCoordinates(cc);
I wrote the lines
j = 1;
prelength = 1000;
removal_loc = nan(1,prelength);
counter = 0;
while j<=length(M)
counter = counter + 1;
removal_loc(counter) = j;
j = j + M(2,j) + 1;
end
removal_loc(isnan(removal_loc)) = [];
M(:,removal_loc) = [];
imagZscale = imag(N(M(1,:),M(2,:),Ps(i)));
which gives me all the x and y points and allows me to get the outputs just from those points. I then modify two of your lines in your loop to be
% accumulate colorscale limits
zrange(1) = min(zrange(1),min(imagZscale));
zrange(2) = max(zrange(2),max(imagZscale));
Now the colorscale is only for values that exist on the contour(s) plotted and not other values in the output of the original function.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Contour Plots 的更多信息

产品


版本

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by