如何在Matlab中将单个2D曲线绘制到颜色图上? [英] How can I plot a single 2-D curve onto a colormap in Matlab?

查看:107
本文介绍了如何在Matlab中将单个2D曲线绘制到颜色图上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Matlab中的现有已编译数据创建了平滑的颜色渐变2-D等高线图.我有一个x轴,y轴和z数据轮廓绘制为颜色图.我的目标是在颜色图上绘制2D曲线,以表示单个z数据值,但我不知道如何.

I have created a smooth color gradient 2-D contour plot with existing compiled data in Matlab. I have an x-axis, y-axis, and z-data contour plotted as a colormap. My goal is to plot a 2-D curve onto the colormap, representing a single z-data value, but I don't know how.

有人知道如何在2D曲线上绘制2D曲线吗?我提供了指向当前颜色图的链接,我希望在该颜色图上绘制单个z值作为曲线.

Does anyone know how to plot a 2-D curve onto a 3-D colormap? I have provided a link to the current colormap onto which I wish to plot a single z-value as a curve.

x = 0.05:0.05:1;
y = 0.0:0.05:1;
[X, Y] = meshgrid(x, y);
Z = [data]
contourf(X, Y, Z);
pcolor(X, Y, Z);
shading interp
title()
xlabel()
ylabel()
colorbar

推荐答案

感谢@ Cris Luengo 引导我访问 contourc 的评论.请注意, contourc 返回等值线.根据文档,

Thanks to @Cris Luengo for his comments directing me to contourc. Notice that contourc returns the isolines. According to the documentation,

要计算级别k的单个轮廓,请使用contourc(Z,[k k])

To compute a single contour of level k, use contourc(Z,[k k])

这意味着我们可以使用
确定等值线的特定值(Z的值) v = [.5 0.75 .85];
然后只需使用循环即可使用

which implies we can identify specific values for the isolines (values of Z) with
v = [.5 0.75 .85];
and then just use a loop to iteratively get the info needed using

for k = 1:length(v)
    Ck = contourc(x,y,Z,[v(k) v(k)]);`
end

这使我们可以在下面的代码中将信息传递给plot(...).

which allows us to pass the info to plot(...) in the code below.

% MATLAB R2018b
x = 0:0.01:1;
y = 0:0.01:1;
[X,Y] = meshgrid(x,y);
Z = sqrt(X.^3+Y);     % Placeholder
v = [.5 0.75 .85];    % Values of Z to plot isolines

figure, hold on
pcolor(X, Y, Z); 
shading interp
colorbar
for k = 1:length(v)
    Ck = contourc(x,y,Z,[v(k) v(k)]);
    plot(Ck(1,2:end),Ck(2,2:end),'k-','LineWidth',2)
end


较旧,但可以使用:将在以上答案确定后删除一次

免责声明:可能有一种更简单的方法.请参阅底部的注释.

Disclaimer: There may be an easier way to do this. See note at bottom.

x = 0:0.01:1;
y = 0:0.01:1;
[X,Y] = meshgrid(x,y);
Z = sqrt(X.^3+Y);          % Placeholder

plot 命令可以覆盖您想要的任何内容最佳.或者,您可以使用 contourf 并指定标记的轮廓,包括突出显示单个轮廓.我已经说明了两种方法-我相信您正在寻找的方法会使用hold on; plot(...),如下所示&和图片.

The plot command can overlay anything you want on top. Or you can use contourf and specify the contours marked, including highlighting individual contours. I've illustrated two approaches—I believe the one you're looking for uses hold on; plot(...) as demonstrated below & with the left image.

在左图中,您将看到我使用了逻辑索引.

In the left figure, you'll see I used logical indexing.

val = 0.75;         % Value of Z to plot contour for
tol = .002;         % numerical tolerance
idxZval = (Z <= val+tol) & (Z >= val-tol);

此方法的成功很大程度上取决于Z上的网格细度以及所需的公差(tol).如果 Z 网格受数据限制,则必须调整公差并根据自己的喜好或数据限制进行调整.我只是进行了调整,直到得到下面的数字,然后才进一步修改.

The success of this approach greatly depends on how fine the mesh is on Z and on the required tolerance (tol). If the Z-mesh is limited by data, then you'll have to adjust the tolerance and tune to your liking or to the limit of your data. I simply adjusted until I got the figure below and didn't tinker further.

% MATLAB 2018b
figure
subplot(1,2,1) % LEFT
    pcolor(X, Y, Z); hold on
    shading interp
    xlabel('X')
    ylabel('Y')
    colorbar

    val = 0.75;         % Value of Z to plot contour for
    tol = .002;         % numerical tolerance
    idxZval = (Z <= val+tol) & (Z >= val-tol);
    plot(X(idxZval),Y(idxZval),'k-','LineWidth',2)
    title('Bootleg approach for Z = 0.75')

subplot(1,2,2) % RIGHT
    v =[0:.1:1.2]        % values of Z to plot as contours
    contourf(X,Y,Z,v)
    colorbar
    title('Contour plot specifying values v =[0:.1:1.2]')


修改:
对于未来访问者,如果XYZ之间的关系是已知的,例如sqrt(X.^3 + Y) = Z,然后可以通过求解方程式直接从该关系(方程式)中提取特定Z值的单个曲线.如果它基于数据而不是分析公式,则可能会更困难,并且上面的方法可能会更容易(由于@ Cris Luengo 在评论中指出了这一点).



For future visitors, if the relationship between X,Y, and Z is known, e.g. sqrt(X.^3 + Y) = Z, then drawing a single curve for a particular Z value may be extracted from that relationship (equation) directly by solving the equation. If it is based on data instead of an analytical formula, then that may be more difficult and the approach above may be easier (thanks to @Cris Luengo for pointing this out in the comments).

这篇关于如何在Matlab中将单个2D曲线绘制到颜色图上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆