@@ -28,12 +28,19 @@ def tripcolor(ax, *args, **kwargs):
28
28
:class:`~matplotlib.tri.Triangulation` for a explanation of these
29
29
possibilities.
30
30
31
- The next argument must be *C*, the array of color values, one per
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.
31
+ The next argument must be *C*, the array of color values, either
32
+ one per point in the triangulation if color values are defined at
33
+ points, or one per triangle in the triangulation if color values
34
+ are defined at triangles. If there are the same number of points
35
+ and triangles in the triangulation it is assumed that color
36
+ values are defined at points unless the kwarg *colorpoints* is
37
+ set to *False*.
38
+
39
+ *shading* may be 'flat' (the default), 'faceted' or 'gouraud'. If
40
+ *shading* is 'flat' or 'faceted' and C values are defined at
41
+ points, the color values used for each triangle are from the mean
42
+ C of the triangle's three points. If *shading* is 'gouraud' then
43
+ color values must be defined at triangles.
37
44
38
45
The remaining kwargs are the same as for
39
46
:meth:`~matplotlib.axes.Axes.pcolor`.
@@ -50,20 +57,28 @@ def tripcolor(ax, *args, **kwargs):
50
57
vmin = kwargs .pop ('vmin' , None )
51
58
vmax = kwargs .pop ('vmax' , None )
52
59
shading = kwargs .pop ('shading' , 'flat' )
60
+ colorpoints = kwargs .pop ('colorpoints' , True )
53
61
54
62
tri , args , kwargs = Triangulation .get_from_args_and_kwargs (* args , ** kwargs )
55
- x = tri .x
56
- y = tri .y
57
- triangles = tri .get_masked_triangles ()
58
-
59
63
C = np .asarray (args [0 ])
60
- if C .shape != x .shape :
61
- raise ValueError ('C array must have same length as triangulation x and'
62
- ' y arrays' )
63
64
64
65
if shading == 'gouraud' :
66
+ if len (C ) != len (tri .x ):
67
+ raise ValueError ('For gouraud shading, the length of C '
68
+ 'array must be the same as the number of '
69
+ 'triangulation points' )
65
70
collection = TriMesh (tri , ** kwargs )
66
71
else :
72
+ if len (C ) != len (tri .x ) and len (C ) != len (tri .triangles ):
73
+ raise ValueError ('Length of C array must be the same as either '
74
+ 'the number of triangulation points or triangles' )
75
+
76
+ # CAtPoints is True if C defined at points
77
+ # or False if C defined at triangles.
78
+ CAtPoints = (len (C ) == len (tri .x ))
79
+ if len (C ) == len (tri .x ) and len (C ) == len (tri .triangles ):
80
+ CAtPoints = colorpoints
81
+
67
82
if shading == 'faceted' :
68
83
edgecolors = (0 ,0 ,0 ,1 ),
69
84
linewidths = (0.25 ,)
@@ -75,10 +90,18 @@ def tripcolor(ax, *args, **kwargs):
75
90
kwargs .setdefault ('linewidths' , linewidths )
76
91
77
92
# 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 )
93
+ maskedTris = tri .get_masked_triangles ()
94
+ verts = np .concatenate ((tri .x [maskedTris ][...,np .newaxis ],
95
+ tri .y [maskedTris ][...,np .newaxis ]), axis = 2 )
96
+
97
+ # Color values.
98
+ if CAtPoints :
99
+ # One color per triangle, the mean of the 3 vertex color values.
100
+ C = C [maskedTris ].mean (axis = 1 )
101
+ elif tri .mask is not None :
102
+ # Remove color values of masked triangles.
103
+ C = C .compress (1 - tri .mask )
104
+
82
105
collection = PolyCollection (verts , ** kwargs )
83
106
84
107
collection .set_alpha (alpha )
0 commit comments