在 Matlab 中绘制类似于使用 Gnuplot 获得的曲面图?

发布于 2024-10-20 12:46:18 字数 1083 浏览 3 评论 0 原文

更新:

链接到我的测试数据集

我有一些这样的数据:

10.0 11.0 0.5
10.0 12.0 0.5
10.0 14.5 0.5
10.0 16.0 0.5
10.0 18.5 0.5
10.0 19.0 0.5
10.0 19.5 0.5
10.0 20.0 0.5

使用Gnuplot,我可以绘制下图:

使用我编写的以下脚本:

set data style lines
set surface
set dgrid3d 30,30
set hidden3d
set ticslevel 0.8
set isosample 40,40

set view 60, 30, 1, 1
set contour base

splot "dataset" using 2:1:($3) notitle

我正在尝试对此图进行一些分析,所以想要将其输入 Matlab,但我不知道如何绘制它。我尝试将数据集导入 Matlab,这就是我所能达到的程度。我正在加载文件并使用以下 Matlab 语句进行绘图:

load data
rangeY = floor(min(data(:,2))):.5:ceil(max(data(:,2)))
rangeX = floor(min(data(:,1))):.5:ceil(max(data(:,1)))
[X,Y] = meshgrid(rangeX,rangeY)
Z = griddata(data(:,1),data(:,2),data(:,3),X,Y, 'cubic')
surf(X,Y,Z)

在此处输入图像描述

图片看起来与我截然不同(轴范围、形状等)有人可以告诉我如何获得类似于我通过 Gnuplot 获得的绘图吗?

Update:

Link to my test dataset

I have some data like this:

10.0 11.0 0.5
10.0 12.0 0.5
10.0 14.5 0.5
10.0 16.0 0.5
10.0 18.5 0.5
10.0 19.0 0.5
10.0 19.5 0.5
10.0 20.0 0.5

Using Gnuplot, I could plot the following picture:

enter image description here

using the following script I wrote:

set data style lines
set surface
set dgrid3d 30,30
set hidden3d
set ticslevel 0.8
set isosample 40,40

set view 60, 30, 1, 1
set contour base

splot "dataset" using 2:1:($3) notitle

I am trying to do some analysis on this graph so wanted to get this into Matlab but am not sure how to draw this. I tried importing the dataset into Matlab and this is how far I could get. I am loading the file and plotting using the following Matlab statements:

load data
rangeY = floor(min(data(:,2))):.5:ceil(max(data(:,2)))
rangeX = floor(min(data(:,1))):.5:ceil(max(data(:,1)))
[X,Y] = meshgrid(rangeX,rangeY)
Z = griddata(data(:,1),data(:,2),data(:,3),X,Y, 'cubic')
surf(X,Y,Z)

enter image description here

The picture looks quite different to me (axis range, shape etc.) Can someone tell me how to get a plot similar to the one I obtained through Gnuplot?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦中的蝴蝶 2024-10-27 12:46:18

surf 无法立即工作的原因是数据非常嘈杂,如果您调用,您会看到

plot3(data(:,1),data(:,2),data(:,3),'.')

因此,您首先需要平滑数据(我猜这就是gnuplot 函数也可以)。以下是平滑数据的一种方法

%# set bin edges (maybe you want to do 5:10:205)
xEdge = 10:10:200;
yEdge = xEdge;

%# bin the data
xId = sum(bsxfun(@ge,data(:,1),xEdge(2:end-1)),2)+1;
yId = sum(bsxfun(@ge,data(:,2),yEdge(2:end-1)),2)+1;

%# average z within every bin
smoothData = accumarray([xId,yId],data(:,3),[length(xCenter),length(yCenter)],@mean);

%# remove bins with no data
smoothData(smoothData==0) = NaN;

%# bin centers
xCenter = (xEdge(1:end-1)+xEdge(2:end))/2;
yCenter = xCenter;

%# plot
meshc(xCenter,yCenter,smoothData)

在此处输入图像描述

The reason surf won't work right away is that the data are quite noisy, as you'll see if you call

plot3(data(:,1),data(:,2),data(:,3),'.')

Thus, you first need to smoothen the data (which is, I guess, what the gnuplot function does as well). Below is one approach to smoothen the data

%# set bin edges (maybe you want to do 5:10:205)
xEdge = 10:10:200;
yEdge = xEdge;

%# bin the data
xId = sum(bsxfun(@ge,data(:,1),xEdge(2:end-1)),2)+1;
yId = sum(bsxfun(@ge,data(:,2),yEdge(2:end-1)),2)+1;

%# average z within every bin
smoothData = accumarray([xId,yId],data(:,3),[length(xCenter),length(yCenter)],@mean);

%# remove bins with no data
smoothData(smoothData==0) = NaN;

%# bin centers
xCenter = (xEdge(1:end-1)+xEdge(2:end))/2;
yCenter = xCenter;

%# plot
meshc(xCenter,yCenter,smoothData)

enter image description here

纵情客 2024-10-27 12:46:18

我很惊讶你的代码不起作用。我在我的一个数据集上尝试了一下,效果很好。作为第一个答案,我建议您发布数据集的链接(如果不敏感),并尝试其他插值方法(从默认的线性插值方法开始)。

然而,我注意到 griddata 在 XY 域的边界产生了相当多的 NaN,尽管我的数据集非常密集并且在该域中分布良好。我检查了文档,其中提到:

有时,griddata 可能会以 NaN 形式返回数据凸包上或非常靠近凸包的点。这是因为计算中的舍入有时会导致难以确定边界附近的点是否在凸包内。

我不认为这是问题的原因,但我仍然怀疑这个函数在非常稀疏的数据集上表现不佳。您应该尝试使用 TriScatteredInterp 来代替,这无论如何都会更快。

I am surprised that your code does not work. I tried it on one of my dataset and it worked ok. As a first answer, I would suggest that your post a link to your dataset (if not sensitive), and that you try other methods of interpolation (starting with the default one, linear).

However, I noticed that griddata produced quite a few NaN at the boundaries of the X-Y domain even though my dataset is quite dense and well distributed in that domain. I checked the doc and it is mentioned that:

Occasionally, griddata might return points on or very near the convex hull of the data as NaNs. This is because roundoff in the computations sometimes makes it difficult to determine if a point near the boundary is in the convex hull.

I don't think this is the cause of your problem, but I still suspect this function not to perform very well with very sparse datasets. You should try TriScatteredInterp instead, which is anyway faster.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文