Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 76ac5c9

Browse files
committed
Update Axes3D.tricontour for custom triangulations
An example has also been added to illustrate the additional flexibility possible with providing a user-defined triangulation.
1 parent ecca88a commit 76ac5c9

2 files changed

Lines changed: 53 additions & 5 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
Contour plots of unstructured triangular grids.
3+
"""
4+
import matplotlib.pyplot as plt
5+
from mpl_toolkits.mplot3d import Axes3D
6+
import matplotlib.tri as tri
7+
import numpy as np
8+
import math
9+
10+
# First create the x and y coordinates of the points.
11+
n_angles = 48
12+
n_radii = 8
13+
min_radius = 0.25
14+
radii = np.linspace(min_radius, 0.95, n_radii)
15+
16+
angles = np.linspace(0, 2*math.pi, n_angles, endpoint=False)
17+
angles = np.repeat(angles[...,np.newaxis], n_radii, axis=1)
18+
angles[:,1::2] += math.pi/n_angles
19+
20+
x = (radii*np.cos(angles)).flatten()
21+
y = (radii*np.sin(angles)).flatten()
22+
z = (np.cos(radii)*np.cos(angles*3.0)).flatten()
23+
24+
# Create a custom triangulation
25+
triang = tri.Triangulation(x, y)
26+
27+
# Mask off unwanted triangles.
28+
xmid = x[triang.triangles].mean(axis=1)
29+
ymid = y[triang.triangles].mean(axis=1)
30+
mask = np.where(xmid*xmid + ymid*ymid < min_radius*min_radius, 1, 0)
31+
triang.set_mask(mask)
32+
33+
plt.figure()
34+
plt.gca(projection='3d')
35+
plt.tricontour(triang, z)
36+
plt.show()

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1980,8 +1980,8 @@ def contour(self, X, Y, Z, *args, **kwargs):
19801980

19811981
contour3D = contour
19821982

1983-
def tricontour(self, X, Y, Z, *args, **kwargs):
1984-
'''
1983+
def tricontour(self, *args, **kwargs):
1984+
"""
19851985
Create a 3D contour plot.
19861986
19871987
========== ================================================
@@ -2001,8 +2001,9 @@ def tricontour(self, X, Y, Z, *args, **kwargs):
20012001
20022002
Returns a :class:`~matplotlib.axes.Axes.contour`
20032003
2004-
.. versionadded:: 1.1.0
2005-
'''
2004+
.. versionchanged:: 1.3.0
2005+
Added support for custom triangulations
2006+
"""
20062007

20072008
extend3d = kwargs.pop('extend3d', False)
20082009
stride = kwargs.pop('stride', 5)
@@ -2011,8 +2012,19 @@ def tricontour(self, X, Y, Z, *args, **kwargs):
20112012

20122013
had_data = self.has_data()
20132014

2015+
tri, args, kwargs = Triangulation.get_from_args_and_kwargs(
2016+
*args, **kwargs)
2017+
X = tri.x
2018+
Y = tri.y
2019+
Z = args[0]
2020+
2021+
# We do this so Z doesn't get passed as an arg to Axes.tricontour
2022+
args = args[1:]
2023+
20142024
jX, jY, jZ = art3d.rotate_axes(X, Y, Z, zdir)
2015-
cset = Axes.tricontour(self, jX, jY, jZ, *args, **kwargs)
2025+
tri = Triangulation(jX, jY, tri.triangles, tri.mask)
2026+
2027+
cset = Axes.tricontour(self, tri, jZ, *args, **kwargs)
20162028
self.add_contour_set(cset, extend3d, stride, zdir, offset)
20172029

20182030
self.auto_scale_xyz(X, Y, Z, had_data)

0 commit comments

Comments
 (0)