Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib, avoiding unwanted triangles in 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()

enter image description here

like image 419
Saullo G. P. Castro Avatar asked Apr 30 '13 13:04

Saullo G. P. Castro


1 Answers

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

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.

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.

Two options:

the right way #1

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.

the right way #2

define the triangles without using delaunay triangluation using Triangulation(x,y,triangles=...)

the quick way

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

*I didn't try either of these options

like image 140
Paul Avatar answered Nov 04 '22 18:11

Paul