Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 74bf315

Browse filesBrowse files
committed
Merge branch 'tripcolor-gouraud' of https://github.com/bfroehle/matplotlib into bfroehle-tripcolor-gouraud
Conflicts: lib/matplotlib/tri/tripcolor.py
2 parents 61f155e + 2f13702 commit 74bf315
Copy full SHA for 74bf315

File tree

Expand file treeCollapse file tree

2 files changed

+93
-21
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+93
-21
lines changed

‎lib/matplotlib/collections.py

Copy file name to clipboardExpand all lines: lib/matplotlib/collections.py
+69-1Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,74 @@ def set_paths(self, patches):
11301130
for p in patches]
11311131
self._paths = paths
11321132

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

11341202
class QuadMesh(Collection):
11351203
"""
@@ -1316,7 +1384,7 @@ def draw(self, renderer):
13161384

13171385

13181386
patchstr = artist.kwdoc(Collection)
1319-
for k in ('QuadMesh', 'PolyCollection', 'BrokenBarHCollection',
1387+
for k in ('QuadMesh', 'TriMesh', 'PolyCollection', 'BrokenBarHCollection',
13201388
'RegularPolyCollection', 'PathCollection',
13211389
'StarPolygonCollection', 'PatchCollection',
13221390
'CircleCollection', 'Collection',):

‎lib/matplotlib/tri/tripcolor.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tri/tripcolor.py
+24-20Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from __future__ import print_function
2-
from matplotlib.collections import PolyCollection
2+
from matplotlib.collections import PolyCollection, TriMesh
33
from matplotlib.colors import Normalize
44
from matplotlib.tri.triangulation import Triangulation
55
import numpy as np
@@ -29,8 +29,11 @@ def tripcolor(ax, *args, **kwargs):
2929
possibilities.
3030
3131
The next argument must be *C*, the array of color values, one per
32-
point in the triangulation. The colors used for each triangle
33-
are from the mean C of the triangle's three points.
32+
point in the triangulation.
33+
34+
*shading* may be 'flat', 'faceted' or 'gouraud'. If *shading* is
35+
'flat' or 'faceted', the colors used for each triangle are from
36+
the mean C of the triangle's three points.
3437
3538
The remaining kwargs are the same as for
3639
:meth:`~matplotlib.axes.Axes.pcolor`.
@@ -53,29 +56,30 @@ def tripcolor(ax, *args, **kwargs):
5356
y = tri.y
5457
triangles = tri.get_masked_triangles()
5558

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

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

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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.