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

Skip to content

Commit 2f13702

Browse files
committed
Add 'gouraud' shading option to tripcolor.
1 parent b269839 commit 2f13702

File tree

2 files changed

+93
-21
lines changed

2 files changed

+93
-21
lines changed

lib/matplotlib/collections.py

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,74 @@ def set_paths(self, patches):
11291129
for p in patches]
11301130
self._paths = paths
11311131

1132+
class TriMesh(Collection):
1133+
"""
1134+
Class for the efficient drawing of a triangular mesh using
1135+
Gouraud shading.
1136+
1137+
A triangular mesh is a :class:`~matplotlib.tri.Triangulation`
1138+
object.
1139+
"""
1140+
def __init__(self, triangulation, **kwargs):
1141+
Collection.__init__(self, **kwargs)
1142+
self._triangulation = triangulation;
1143+
self._shading = 'gouraud'
1144+
self._is_filled = True
1145+
1146+
self._bbox = transforms.Bbox.unit()
1147+
1148+
# Unfortunately this requires a copy, unless Triangulation
1149+
# was rewritten.
1150+
xy = np.hstack((triangulation.x.reshape(-1,1),
1151+
triangulation.y.reshape(-1,1)))
1152+
self._bbox.update_from_data_xy(xy)
1153+
1154+
def get_paths(self):
1155+
if self._paths is None:
1156+
self.set_paths()
1157+
return self._paths
1158+
1159+
def set_paths(self):
1160+
self._paths = self.convert_mesh_to_paths(self._triangulation)
1161+
1162+
@staticmethod
1163+
def convert_mesh_to_paths(tri):
1164+
"""
1165+
Converts a given mesh into a sequence of
1166+
:class:`matplotlib.path.Path` objects for easier rendering by
1167+
backends that do not directly support meshes.
1168+
1169+
This function is primarily of use to backend implementers.
1170+
"""
1171+
Path = mpath.Path
1172+
triangles = tri.get_masked_triangles()
1173+
verts = np.concatenate((tri.x[triangles][...,np.newaxis],
1174+
tri.y[triangles][...,np.newaxis]), axis=2)
1175+
return [Path(x) for x in verts]
1176+
1177+
@allow_rasterization
1178+
def draw(self, renderer):
1179+
if not self.get_visible(): return
1180+
renderer.open_group(self.__class__.__name__)
1181+
transform = self.get_transform()
1182+
1183+
# Get a list of triangles and the color at each vertex.
1184+
tri = self._triangulation
1185+
triangles = tri.get_masked_triangles()
1186+
1187+
verts = np.concatenate((tri.x[triangles][...,np.newaxis],
1188+
tri.y[triangles][...,np.newaxis]), axis=2)
1189+
1190+
self.update_scalarmappable()
1191+
colors = self._facecolors[triangles];
1192+
1193+
gc = renderer.new_gc()
1194+
self._set_gc_clip(gc)
1195+
gc.set_linewidth(self.get_linewidth()[0])
1196+
renderer.draw_gouraud_triangles(gc, verts, colors, transform.frozen())
1197+
gc.restore()
1198+
renderer.close_group(self.__class__.__name__)
1199+
11321200

11331201
class QuadMesh(Collection):
11341202
"""
@@ -1315,7 +1383,7 @@ def draw(self, renderer):
13151383

13161384

13171385
patchstr = artist.kwdoc(Collection)
1318-
for k in ('QuadMesh', 'PolyCollection', 'BrokenBarHCollection',
1386+
for k in ('QuadMesh', 'TriMesh', 'PolyCollection', 'BrokenBarHCollection',
13191387
'RegularPolyCollection', 'PathCollection',
13201388
'StarPolygonCollection', 'PatchCollection',
13211389
'CircleCollection', 'Collection',):

lib/matplotlib/tri/tripcolor.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from matplotlib.collections import PolyCollection
1+
from matplotlib.collections import PolyCollection, TriMesh
22
from matplotlib.colors import Normalize
33
from matplotlib.tri.triangulation import Triangulation
44
import numpy as np
@@ -28,8 +28,11 @@ def tripcolor(ax, *args, **kwargs):
2828
possibilities.
2929
3030
The next argument must be *C*, the array of color values, one per
31-
point in the triangulation. The colors used for each triangle
32-
are from the mean C of the triangle's three points.
31+
point in the triangulation.
32+
33+
*shading* may be 'flat', 'faceted' or 'gouraud'. If *shading* is
34+
'flat' or 'faceted', the colors used for each triangle are from
35+
the mean C of the triangle's three points.
3336
3437
The remaining kwargs are the same as for
3538
:meth:`~matplotlib.axes.Axes.pcolor`.
@@ -52,29 +55,30 @@ def tripcolor(ax, *args, **kwargs):
5255
y = tri.y
5356
triangles = tri.get_masked_triangles()
5457

55-
# Vertices of triangles.
56-
verts = np.concatenate((x[triangles][...,np.newaxis],
57-
y[triangles][...,np.newaxis]), axis=2)
58-
5958
C = np.asarray(args[0])
6059
if C.shape != x.shape:
6160
raise ValueError('C array must have same length as triangulation x and'
6261
' y arrays')
6362

64-
# Color values, one per triangle, mean of the 3 vertex color values.
65-
C = C[triangles].mean(axis=1)
66-
67-
if shading == 'faceted':
68-
edgecolors = (0,0,0,1),
69-
linewidths = (0.25,)
63+
if shading == 'gouraud':
64+
collection = TriMesh(tri, **kwargs)
7065
else:
71-
edgecolors = 'face'
72-
linewidths = (1.0,)
73-
kwargs.setdefault('edgecolors', edgecolors)
74-
kwargs.setdefault('antialiaseds', (0,))
75-
kwargs.setdefault('linewidths', linewidths)
76-
77-
collection = PolyCollection(verts, **kwargs)
66+
if shading == 'faceted':
67+
edgecolors = (0,0,0,1),
68+
linewidths = (0.25,)
69+
else:
70+
edgecolors = 'face'
71+
linewidths = (1.0,)
72+
kwargs.setdefault('edgecolors', edgecolors)
73+
kwargs.setdefault('antialiaseds', (0,))
74+
kwargs.setdefault('linewidths', linewidths)
75+
76+
# Vertices of triangles.
77+
verts = np.concatenate((x[triangles][...,np.newaxis],
78+
y[triangles][...,np.newaxis]), axis=2)
79+
# Color values, one per triangle, mean of the 3 vertex color values.
80+
C = C[triangles].mean(axis=1)
81+
collection = PolyCollection(verts, **kwargs)
7882

7983
collection.set_alpha(alpha)
8084
collection.set_array(C)

0 commit comments

Comments
 (0)