@@ -33,8 +33,8 @@ def tripcolor(ax, *args, **kwargs):
3333 points, or one per triangle in the triangulation if color values
3434 are defined at triangles. If there are the same number of points
3535 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 *.
36+ values are defined at points; to force the use of color values at
37+ triangles use the kwarg *facecolors*=C instead of just *C *.
3838
3939 *shading* may be 'flat' (the default) or 'gouraud'. If *shading*
4040 is 'flat' and C values are defined at points, the color values
@@ -58,10 +58,32 @@ def tripcolor(ax, *args, **kwargs):
5858 vmin = kwargs .pop ('vmin' , None )
5959 vmax = kwargs .pop ('vmax' , None )
6060 shading = kwargs .pop ('shading' , 'flat' )
61- colorpoints = kwargs .pop ('colorpoints ' , True )
61+ facecolors = kwargs .pop ('facecolors ' , None )
6262
6363 tri , args , kwargs = Triangulation .get_from_args_and_kwargs (* args , ** kwargs )
64- C = np .asarray (args [0 ])
64+
65+ # C is the colors array, defined at either points or faces (i.e. triangles).
66+ # If facecolors is None, C are defined at points.
67+ # If facecolors is not None, C are defined at faces.
68+ if facecolors is not None :
69+ C = facecolors
70+ else :
71+ C = np .asarray (args [0 ])
72+
73+ # If there are a different number of points and triangles in the
74+ # triangulation, can omit facecolors kwarg as it is obvious from
75+ # length of C whether it refers to points or faces.
76+ # Do not do this for gouraud shading.
77+ if facecolors is None and len (C ) == len (tri .triangles ) and \
78+ len (C ) != len (tri .x ) and shading != 'gouraud' :
79+ facecolors = C
80+
81+ # Check length of C is OK.
82+ if (facecolors is None and len (C ) != len (tri .x )) or \
83+ (facecolors is not None and len (C ) != len (tri .triangles )):
84+ raise ValueError ('Length of color values array must be the same '
85+ 'as either the number of triangulation points '
86+ 'or triangles' )
6587
6688
6789 # Handling of linewidths, shading, edgecolors and antialiased as
@@ -86,29 +108,22 @@ def tripcolor(ax, *args, **kwargs):
86108
87109
88110 if shading == 'gouraud' :
111+ if facecolors is not None :
112+ raise ValueError ('Gouraud shading does not support the use '
113+ 'of facecolors kwarg' )
89114 if len (C ) != len (tri .x ):
90- raise ValueError ('For gouraud shading, the length of C '
91- 'array must be the same as the number of '
92- 'triangulation points' )
115+ raise ValueError ('For gouraud shading, the length of color '
116+ 'values array must be the same as the '
117+ 'number of triangulation points' )
93118 collection = TriMesh (tri , ** kwargs )
94119 else :
95- if len (C ) != len (tri .x ) and len (C ) != len (tri .triangles ):
96- raise ValueError ('Length of C array must be the same as either '
97- 'the number of triangulation points or triangles' )
98-
99- # CAtPoints is True if C defined at points
100- # or False if C defined at triangles.
101- CAtPoints = (len (C ) == len (tri .x ))
102- if len (C ) == len (tri .x ) and len (C ) == len (tri .triangles ):
103- CAtPoints = colorpoints
104-
105120 # Vertices of triangles.
106121 maskedTris = tri .get_masked_triangles ()
107122 verts = np .concatenate ((tri .x [maskedTris ][...,np .newaxis ],
108123 tri .y [maskedTris ][...,np .newaxis ]), axis = 2 )
109124
110125 # Color values.
111- if CAtPoints :
126+ if facecolors is None :
112127 # One color per triangle, the mean of the 3 vertex color values.
113128 C = C [maskedTris ].mean (axis = 1 )
114129 elif tri .mask is not None :
0 commit comments