本文介绍了Matplotlib,避免在plot_trisurf()中出现不需要的三角形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码来创建一个圆锥体,稍后将对其应用位移场.在下图中,您可以看到顶部绘制了一些大三角形,但底部未绘制.我相信有一些内部隐藏参数可以告诉plot_trisurf()三角形应创建的距离,否则它们也应该在底部创建.

I have the following code to create a cone for which a displacement field will be applied later on. In the figure shown below you can see that some big triangles are drawn at the top, but not at the bottom. I believe there is some internal hidden parameter that tells plot_trisurf() up to which distance the triangles should be created, otherwise they should have been created at the bottom too.

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from numpy import linspace, interp, meshgrid, sin, cos, pi
numel_circum = 120
L = 38.
rb, rt = (38., 16.)
t1, t2 = (0, 2*pi)
rav = ( rb - rt )*rt/rb + rt
perav = (t2-t1) * rav
elsize_L = perav / numel_circum
numel_L = int(round(L/elsize_L,0))
ts = linspace(t1, t2, numel_circum)
r = lambda z: interp( z, [0, L], [rb, rt] )
zs = linspace(0, L, numel_L)
ts, zs = meshgrid( ts, zs )
ys = r(zs)*sin(ts)
xs = r(zs)*cos(ts)
# plotting
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_trisurf(xs.flatten(), ys.flatten(), zs.flatten(),
                cmap=cm.jet, linewidth=0.2)
plt.show()

推荐答案

到目前为止,您可能已经自己解决或解决了这个问题,但是我想我会为未来的访问者们提供一个答案.

You have probably moved on or solved this yourself by now, but I thought I'd throw an answer up for future vistors.

在较小的圆中获得三角形的原因不是最大距离,这是因为这些三角形包含在点投影在x,y平面上的凸包中.

The reason you are getting triangles in the smaller circle is not a max-distance thing, it is because those triangles are contained within the convex hull of your points projection on the x,y plane.

如果查看plot_trisurf源(numpy.source(Axes3D.plot_trisurf)),您会发现它每次都执行delaunay三角剖分,并且没有机会定义三角形或排除不需要的三角形.

If you look at the plot_trisurf source, (numpy.source(Axes3D.plot_trisurf)) you'll see that it performs delaunay triangulation every time and there is no opportunity to define your triangles or exclude unwanted triangles.

两个选项:

复制 plot_trisurf 到脚本中,并添加 tri.set_mask(...) (tri是一个matplotlib.tri.triangulation.Triangulation实例),使用您选择的算法(一些最大边长标准或找到质心在某个半径内的三角形.无论适合您的实际数据)在创建布尔掩码之后三角剖分完成.

copy the source of plot_trisurf to your script and add the line tri.set_mask(...) (tri is a matplotlib.tri.triangulation.Triangulation instance) using the algo of your choice (some max edge length criteria or find triangles who centroids are within some radius.. whatever suits your actual data) to create the boolean mask after triangulation is done.

使用Triangulation(x,y,triangles=...)

将plot_trisurf vmax设置为略低于圆平面.

set plot_trisurf vmax to be slightly below the plane of the circle.

*我没有尝试过这些选项中的任何一个

*I didn't try either of these options

这篇关于Matplotlib,避免在plot_trisurf()中出现不需要的三角形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 12:01