Matplotlib的tripcolor存在问题吗?

3
我希望使用matplotlib.pyplot中的tripcolor函数来查看我的一些数据的彩色轮廓。这些数据是从z = cst处的XY平面中使用Paraview提取出来的。我直接从Paraview以csv格式导出数据,它会为我三角化平面。
问题在于,根据平面位置(即网格),tripcolor有时会给出好的结果,有时会给出不好的结果。
以下是一个简单的示例代码和结果,用于说明此问题: 代码
import matplotlib.pyplot as plt
import numpy as np

p,u,v,w,x,y,z  = np.loadtxt('./bad.csv',delimiter=',',skiprows=1,usecols=(0,1,2,3,4,5,6),unpack=True)

NbLevels = 256

plt.figure()
plt.gca().set_aspect('equal')

plt.tripcolor(x,y,w,NbLevels,cmap=plt.cm.hot_r,edgecolor='black')

cbar = plt.colorbar()
cbar.set_label('Velocity magnitude',labelpad=10)

plt.show()

tripcolor的结果

enter image description here

这是导致问题的文件

我听说matplotlib的tripcolor有时会出现错误,那么这是一个错误吗?

2个回答

8

正如@Hooked所强调的那样,这是Delaunay三角剖分的正常行为。如果要删除不需要的三角形,则应通过显式传递三角形来提供自己的三角剖分

在您的情况下,这很容易实现,因为您的数据几乎是有结构的:建议在平面上执行Delaunay三角剖分(r,theta),然后将这些三角形传递给初始的(x,y)数组。您可以利用内置的TriAnalyzer类从(r,theta)三角剖分中删除非常平坦的三角形(它们可能由于舍入误差而存在)。

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.tri as mtri

p,u,v,w,x,y,z  = np.loadtxt('./bad.csv',delimiter=',',skiprows=1,usecols=(0,1,2,3,4,5,6),unpack=True)

r = np.sqrt(y**2 + x**2)
tan = (y / x)
aux_tri = mtri.Triangulation(r/np.max(r), tan/np.max(tan))
triang = mtri.Triangulation(x, y, aux_tri.triangles)
triang.set_mask(mtri.TriAnalyzer(aux_tri).get_flat_tri_mask())

NbLevels = 256

plt.figure()
plt.gca().set_aspect('equal')

plt.tripcolor(triang, w, NbLevels, cmap=plt.cm.jet, edgecolor='black')

cbar = plt.colorbar()
cbar.set_label('Velocity magnitude',labelpad=10)
plt.show()

enter image description here


@Ragloo 确保您给出好的答案点赞,并且如果它解决了您的问题,请接受它! - Hooked
@Hooked 感谢您的建议,不幸的是我只有不到15个声望,所以目前无法为答案点赞。 - Ragloo

2
这可能是因为Paraview调用的Delaunay三角剖分创建了点的凸包(应该如此)。为了测试这个假设,我使用了matplotlib.tri.Triangulation函数,并绘制了从x-y值得到的结果网格:
import matplotlib.tri as tri
plt.scatter(x,y)
w[:] = 1
triang = tri.Triangulation(x, y)
plt.tripcolor(triang,w,alpha=.2)

enter image description here

这段话的翻译是:“这表明有同样的效果。可以通过手动或使用非凸边界查找器从网格中去除不需要的三角形。”

感谢Hooked提供的建议,以及在我的问题中包含图片,这样更好看:) - Ragloo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接