我的教授给了我一个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方法。在
以下是对所发生情况的一些解释,并提供一个示例函数:
^{pr2}$
Cases
语句从规范化绘图中提取每一行。(Normal
将图形形式简化为Cases
)有17行独立的Line[List[{x, y}, {x, y}, ...]]
。在对于每一行,
List[{x, y}, {x, y}, ...]
由pts
表示,因此将
pts
的每个列表乘以xdiv
。在这些线可以打印出来。在
相关问题 更多 >
编程相关推荐