如何在不进行三角测量、匹配pcolormesh的情况下绘制粗糙数据集的轮廓?
我有一个非常粗略的数据集,用于使用 matplotlib 的 pcolormesh。 x,y 是 2D numpy 数组,代表当前的统一网格。 z
包含范围从 1
-9
的整数,每个数字匹配一个相位。选项 shading='nearest'
将根据 z
选择的颜色置于 (x,y)
的中心。我的颜色图是根据可能的 z
值进行分段的。
vmin, vmax = 1, 9
colors = ['blue', 'orange', 'black', 'gray', 'cyan', 'lime', 'yellow', 'green', 'red']
cmap = ListedColormap(colors)
axes[0].pcolormesh(x, y, z, shading = 'nearest', vmin = vmin, vmax = vmax, cmap = cmap)
但是,某些相具有共同的属性,这就是我想添加轮廓的原因。例如,我想绘制一个将彩色部分和黑色/灰色部分分开的轮廓。我这里有两个问题:
- 如果我可以使用 contour< /a> 但我无法做到这一点,请参阅用 contourf,没有它对我的数据进行三角测量(?)。如果我有更多的数据点,这不会成为问题,但我不太可能大幅提高分辨率。即使我可以接受三角测量:不应绘制黄色区域。但由于 z 从
z=8
(绿色)跳转到z = 6
(石灰),轮廓会插入一个中间黄色区域。 - 取决于我们如何解决这个问题:我真的希望能够为连接和断开区域绘制轮廓。
我的一个想法是定义一个涵盖石灰、绿色和青色的新阶段,然后勾勒出该区域的轮廓。数据操作很简单,但是,我不知道之后如何继续使用 matplotlib。此外,我不知道如何识别连接和断开的单元格。
I have a pretty rough data set I am using to draw a phase diagram with matplotlib's pcolormesh.x,y
are 2D numpy arrays and represent a uniform grid at the moment. z
contains integers ranging from 1
-9
, each number matching a phase. The option shading='nearest'
centers the color chosen according to z
at (x,y)
. My colormap is segmented matching the possible z
values.
vmin, vmax = 1, 9
colors = ['blue', 'orange', 'black', 'gray', 'cyan', 'lime', 'yellow', 'green', 'red']
cmap = ListedColormap(colors)
axes[0].pcolormesh(x, y, z, shading = 'nearest', vmin = vmin, vmax = vmax, cmap = cmap)
With this I get the upper subplot which is acceptable for me.
However, some of the phases have common properties which is why I would like to add contours. For example, I would like to draw a contour that separates the colored and the black/gray parts. I have two problems here:
- It would be great if I could use contour but I cannot manage to do so, see the second subplot drawn with contourf, without it triangulating(?) my data. This would not be a problem if I had a lot more data points but it is unlikely that I will increase the resolution by much. Even if I could live with triangulation: No yellow area should be drawn. But since z jumps from
z=8
(green) toz = 6
(lime) contour inserts an intermediate yellow area. - Depending on how we solve this problem: I would really like to be able to draw contours both for connected and disconnected areas.
An idea I have is defining a new phase that covers lime, green and cyan and then outline that area. The data manipulation for this is simple, however, I do not know how to proceed with matplotlib after that. Besides, I do not know how one would identify connected and disconnected cells.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我设法完成了以下我几乎满意的设置:
关键是所谓的 <强>阿尔法形状。它本质上是三角测量,粗略地说是为了确定一组点的边界多边形。 这里是对应的python模块。实施起来非常简单。我之前没有任何关于 Shapely 的经验。此外,我还必须深入研究 matplotlib 的 pcolor源代码。最后我想出了以下脚本,主要代码在底部。
几乎所有定义的函数都取自matplotlib的 pcolor 和 _pcolorargs。
选择的 Alpha 越大,Alpha 形状就会变得越详细。对于非常小的 alpha,你会得到一个凸包。我附上脚本的结果。
,轮廓与蓝色区域不完全匹配。如果 alpha 变得太大,如果我理解正确的话,alpha 形状将不会返回正确的多边形,这就是为什么我无法使轮廓对齐得更紧密。我认为这也与我的数据的规则间距有关。
I managed to come of with the following setup that I am almost satisfied with:
The key is the so-called alpha shape. It is in essence a triangulation, roughly speaking for determining the bounding polygon of a set of points. Here is the corresponding python module. It was very simple to implement. I had no prior experience with shapely. In addition, I had to dig a bit into matplotlib's pcolor source code. In the end I came up with the following script, main code is at the bottom.
Almost all of the defined functions are taken from matplotlib's pcolor and _pcolorargs.
The alpha shape will become more detailed the larger you choose alpha. For very small alpha you will get a convex hull. I am attaching the result of the script.
As you can see, the contour does not exactly match the blue area. If alpha becomes too large, alpha shape will not return a proper polygon if I understood it correctly which is why I cannot make the contour align even tighter. I think it has something to do with the regular spacing of my data, too.