了解Mathematica等高线提取

问题描述 投票:1回答:1

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

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中编写类似的代码并理解如何。另外我想知道我是否可以在不使用ContourPlot函数的情况下提取线条。我对绘制轮廓并不特别感兴趣,我只需要一个列表。

编辑:改述问题。另外:matplotlib - extracting data from contour lines似乎解释了如何在python中实现我的目标。但是,我真的不明白它是如何做它的,并判断在性能方面是否有更好的方法来实现它(我处理非常大的列表,所以这个轮廓提取似乎是瓶颈) 。我正在寻找更多解释来了解究竟发生了什么。提供的答案非常适合解释Mathematica代码。我将详细介绍matplotlib方法。

python export wolfram-mathematica equivalent
1个回答
2
投票

以下是对正在发生的事情的一些解释,以及一个示例函数:

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

cases = Cases[normalplot, Line[pts_, ___], Infinity];
Length[cases]
First[cases]
17
Line[{{16.2216, 1.}, {17., 1.29614}, ... , {16.7818, 160.782}, {16.2216, 161.}}]

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

对于每条线,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

© www.soinside.com 2019 - 2024. All rights reserved.