了解Mathematica等高线提取

2024-09-24 00:31:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我的教授给了我一个Mathematica笔记本,在那里他绘制了一个二维轮廓,然后将轮廓提取为直线(即坐标元组列表)。特定的代码段如下所示:

xdiv = 0.05
zt = 1.
lis = Table[SomeFunction[x, y, zt], {y, -xmax, xmax, xdiv}, {x, -xmax, xmax, xdiv}];
plot = ListContourPlot[lis, PlotLegends -> Automatic, Contours -> {0}, ContourShading -> None];
lines = Cases[Normal@plot, Line[pts_, ___] :> xdiv*pts, Infinity];

我不完全理解代码到底是怎么做的,我正在寻求帮助解释。我想用python编写一个类似的代码,并理解如何编写。我还想知道我是否可以在不使用轮廓图功能的情况下提取线条。我对绘制等高线不是特别感兴趣,我只需要它的线条列表。在

编辑:重新措辞问题。 另外:matplotlib - extracting data from contour lines似乎解释了如何在python中实现我的目标。但是,我不太明白它是如何做到的,也不知道在性能方面是否有更好的方法来实现它(我处理的是非常大的列表,因此这种轮廓提取似乎是一个瓶颈)。我在寻找更多的解释来理解到底发生了什么。提供的答案很好地解释了Mathematica代码。我将进一步了解matplotlib方法。在


Tags: 方法代码列表plotmatplotlib绘制pts线条
1条回答
网友
1楼 · 发布于 2024-09-24 00:31:33

以下是对所发生情况的一些解释,并提供一个示例函数:

SomeFunction[x_, y_, zt_] := Sin[3 x + y^2]
xmax = 4;

xdiv = 0.05;
zt = 1.;
lis = Table[SomeFunction[x, y, zt], {y, -xmax, xmax, xdiv}, {x, -xmax, xmax, xdiv}];
plot = ListContourPlot[lis, PlotLegends -> Automatic, Contours -> {0},
  ContourShading -> None];
normalplot = Normal@plot

enter image description here

^{pr2}$
17
Line[{{16.2216, 1.}, {17., 1.29614}, ... , {16.7818, 160.782}, {16.2216, 161.}}]

Cases语句从规范化绘图中提取每一行。(Normal将图形形式简化为Cases)有17行独立的Line[List[{x, y}, {x, y}, ...]]。在

对于每一行,List[{x, y}, {x, y}, ...]pts表示,因此

lines = Cases[normalplot, Line[pts_, ___] :> xdiv*pts, Infinity]

pts的每个列表乘以xdiv。在

0.05 * {{16.2216, 1.}, {17., 1.29614}, ... , {16.7818, 160.782}, {16.2216, 161.}}
= {{0.81108, 0.05}, {0.85, 0.0648069}, ... {0.839088, 8.03909}, {0.81108, 8.05}}

这些线可以打印出来。在

ListLinePlot[lines, AspectRatio -> 1]

enter image description here

相关问题 更多 >