英文:
Plot Contour with Gaps in GNUPlot C++? (C++ 14, VS 22)
问题
我正在尝试在GNUPlot
C++库中绘制等高线图,但我希望能够在数据中留下空白(在没有数据的地方不进行插值)。
我正在使用C++ 14和Visual Studio 2022。
我有以下示例代码:
#include <iostream>
#include <string>
#include <vector>
#include "gnuplot-iostream.h"
using namespace std;
int main()
{
vector<vector<float>> data = {};
for (float x = -5; x <= 5; x += 0.1)
{
for (float y = -5; y <= 5; y += 0.1)
{
float dist = sqrt(pow(x, 2) + pow(y, 2));
float z = 1 / dist;
if ((dist > 0.1) && (z < 0.45))
data.push_back(vector<float>{x, y, z});
}
}
Gnuplot gp;
gp << "set title 'test'\n";
gp << "set dgrid3d 100,100\n";
gp << "splot" << gp.file1d(data) << "with pm3d title 'test'" << endl;
cin.get();
return 0;
}
这将产生以下等高线图。
然而,在上面的图中,中间的"坑"实际上没有任何数据:
该函数会自动在没有数据的区域进行插值,以创建等高线图。
有没有办法阻止等高线函数进行插值,使得GNUPlot
的等高线函数在没有数据的区域保持空白,而不是进行插值呢?
目前,我正在使用dgrid3d
函数来创建我的等高线网格,但似乎无法通过该函数实现我的目标。有没有其他更适合我尝试实现目标的绘图函数呢?
感谢阅读我的帖子,任何指导都将不胜感激。
英文:
I'm attempting to plot a contour plot in the GNUPlot
C++ library, however I want to be able to plot with holes in the data (without interpolation where there's no data).
I'm using C++ 14, with Visual Studio 2022.
I have the following example code:
#include <iostream>
#include <string>
#include <vector>
#include "gnuplot-iostream.h"
using namespace std;
int main()
{
vector<vector<float>> data = {};
for (float x = -5; x <= 5; x += 0.1)
{
for (float y = -5; y <= 5; y += 0.1)
{
float dist = sqrt(pow(x, 2) + pow(y, 2));
float z = 1 / dist;
if ((dist > 0.1) && (z < 0.45))
data.push_back(vector<float>{x, y, z});
}
}
Gnuplot gp;
gp << "set title 'test'\n";
gp << "set dgrid3d 100,100\n";
gp << "splot" << gp.file1d(data) << "with pm3d title 'test'" << endl;
cin.get();
return 0;
}
Which produces the following contour plot:
However, in the plot above, the middle "crater" doesn't actually have any data in it:
The function automatically interpolates any areas without data to create the contour plot.
Is it possible in any way to stop that from happening with the contour functions? So the GNUPlot
contour functions leave any areas without data empty, instead of interpolating?
Currently I'm using the dgrid3d
function to create my contour grid, however it does not appear to be possible to achieve my goal with that. Is there a different graphing function that would be better suited to what I'm trying to accomplish?
Thanks for reading my post, any guidance is appreciated.
答案1
得分: 1
以下是翻译好的内容:
"First a comment that 'contour plot' means something else entirely to gnuplot. There are no contours in this plot."
"首先,要注意 'contour plot' 对于 gnuplot 来说意味着完全不同的东西。这个图中没有轮廓。"
"The issue here is that by setting dgrid3d
you reconstruct the full grid with no hole in it. Don't do that. You are generating the data as a grid anyhow, it's just that some of the grid points have a special status. Here are two ways to do it in gnuplot directly. Note that dgrid3d
is not used for either method. I don't know anything about iostream so I leave that layer of coding to you."
"这里的问题是,通过设置 dgrid3d
,你重新构建了一个没有空隙的完整网格。不要这样做。无论如何,你正在生成数据作为一个网格,只是其中一些网格点具有特殊状态。这里有两种在 gnuplot 中直接执行的方法。请注意,这两种方法都不使用 dgrid3d
。至于 iostream,我对此一无所知,所以我把这一层编码交给你。"
"Method 1 - Write NaN ('not-a-number') for the points to be omitted."
"方法1 - 对于要省略的点,写入 NaN('not-a-number')。"
"Method 2 - Write the z value for all points but tell gnuplot to clip at z=0.45"
"方法2 - 为所有点写入 z 值,但告诉 gnuplot 在 z=0.45 处截断。"
英文:
First a comment that "contour plot" means something else entirely to gnuplot. There are no contours in this plot.
The issue here is that by setting dgrid3d
you reconstruct the full grid with no hole in it. Don't do that. You are generating the data as a grid anyhow, it's just that some of the grid points have a special status. Here are two ways to do it in gnuplot directly. Note that dgrid3d
is not used for either method. I don't know anything about iostream so I leave that layer of coding to you.
Method 1 -
Write NaN ("not-a-number") for the points to be omitted.
set samples 101,101
set isosamples 101,101
set urange [-5:5]
set vrange [-5:5]
dist(x,y) = sqrt(x**2 + y**2)
z(x,y) = 1/dist(x,y)
splot '++' using (dist($1,$2)>0.1 && z($1,$2)<0.45 ? $1 : NaN):2:(z($1,$2)) with pm3d
Method 2 -
Write the z value for all points but tell gnuplot to clip at z=0.45
set samples 101,101
set isosamples 101,101
set urange [-5:5]
set vrange [-5:5]
dist(x,y) = sqrt(x**2 + y**2)
z(x,y) = 1/dist(x,y)
set zrange [*:0.45]
splot '++' using 1:2:(z($1,$2)) with pm3d
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论