@@ -21,62 +21,83 @@ def tripcolor(ax, *args, alpha=1.0, norm=None, cmap=None, vmin=None,
2121 optionally the *triangles* and a *mask*. See `.Triangulation` for an
2222 explanation of these parameters.
2323
24+ If neither of *triangulation* or *triangles* are given, the triangulation
25+ is calculated on the fly. In this case, it does not make sense to provide
26+ colors at the triangle faces via *C* or *facecolors* because there are
27+ multiple possible triangulations for a group of points and you don't know
28+ which triangles will be constructed.
29+
2430 Parameters
2531 ----------
2632 triangulation : `.Triangulation`
2733 An already created triangular grid.
2834 x, y, triangles, mask
2935 Parameters specifying defining the triangular grid. See
30- `.Triangulation`.
31-
32-
33- The next argument must be *C*, the array of color values, either
34- one per point in the triangulation if color values are defined at
35- points, or one per triangle in the triangulation if color values
36- are defined at triangles. If there are the same number of points
37- and triangles in the triangulation it is assumed that color
38- values are defined at points; to force the use of color values at
39- triangles use the kwarg ``facecolors=C`` instead of just ``C``.
40-
41- *shading* may be 'flat' (the default) or 'gouraud'. If *shading*
42- is 'flat' and C values are defined at points, the color values
43- used for each triangle are from the mean C of the triangle's
44- three points. If *shading* is 'gouraud' then color values must be
45- defined at points.
46-
47-
48-
49- tripcolor(x, y, [triangles], C, [mask=mask], ...)
50-
51-
52- The remaining kwargs are the same as for `~.Axes.pcolor`.
36+ `.Triangulation`. This is mutually exclusive with specifying
37+ *triangulation*.
38+ C : array-like
39+ The color values, either for the points or for the triangles. Which one
40+ is automatically inferred from the length of *C*, i.e. does it match
41+ the number of points or the number of triangles. If there are the same
42+ number of points and triangles in the triangulation it is assumed that
43+ color values are defined at points; to force the use of color values at
44+ triangles use the keyword argument ``facecolors=C`` instead of just
45+ ``C``.
46+ This parameter is position-only.
47+ facecolors : array-like, optional
48+ Can be used alternatively to *C* to specify colors at the triangle
49+ faces. This parameter takes precedence over *C*.
50+ shading : {'flat', 'gouraud'}, default: 'flat'
51+ If is 'flat' and the color values *C* are defined at points, the color
52+ values used for each triangle are from the mean C of the triangle's
53+ three points. If *shading* is 'gouraud' then color values must be
54+ defined at points.
55+ other_parameters
56+ All other parameters are the same as for `~.Axes.pcolor`.
57+
58+ Notes
59+ -----
60+ It is possible to pass the triangles positionally, i.e.
61+ ``tripcolor(x, y, triangles, C, ...)``. However, this is discouraged.
62+ For more clarity, pass *triangles* via keyword argument.
5363 """
5464 _api .check_in_list (['flat' , 'gouraud' ], shading = shading )
5565
5666 tri , args , kwargs = Triangulation .get_from_args_and_kwargs (* args , ** kwargs )
5767
58- # C is the colors array defined at either points or faces (i.e. triangles).
59- # If facecolors is None, C are defined at points.
60- # If facecolors is not None, C are defined at faces.
68+ # Parse the color to be in one of (the other variable will be None):
69+ # - facecolors: if specified at the triangle faces
70+ # - point_colors: if specified at the points
6171 if facecolors is not None :
62- C = facecolors
72+ if args :
73+ _api .warn_external (
74+ "Positional parameter C has no effect when the keyword "
75+ "facecolors is given" )
76+ point_colors = None
77+ if len (facecolors ) != len (tri .triangles ):
78+ raise ValueError ("The length of facecolors must match the number "
79+ "of triangles" )
6380 else :
81+ # Color from positional parameter C
82+ if not args :
83+ raise ValueError (
84+ "Missing color parameter. Please pass C positionally or "
85+ "facecolors via keyword" )
86+ elif len (args ) > 1 :
87+ _api .warn_external (
88+ "Additional positional parameters {args[1:]!r} are ignored" )
6489 C = np .asarray (args [0 ])
65-
66- # If there are a different number of points and triangles in the
67- # triangulation, can omit facecolors kwarg as it is obvious from
68- # length of C whether it refers to points or faces.
69- # Do not do this for gouraud shading.
70- if (facecolors is None and len (C ) == len (tri .triangles ) and
71- len (C ) != len (tri .x ) and shading != 'gouraud' ):
72- facecolors = C
73-
74- # Check length of C is OK.
75- if ((facecolors is None and len (C ) != len (tri .x )) or
76- (facecolors is not None and len (C ) != len (tri .triangles ))):
77- raise ValueError ('Length of color values array must be the same '
78- 'as either the number of triangulation points '
79- 'or triangles' )
90+ if len (C ) == len (tri .x ):
91+ # having this before the len(tri.triangles) comparison gives
92+ # precedence to nodes if there are as many nodes as triangles
93+ point_colors = C
94+ facecolors = None
95+ elif len (C ) == len (tri .triangles ):
96+ point_colors = None
97+ facecolors = C
98+ else :
99+ raise ValueError ('The length of C must match either the number '
100+ 'of points or the number of triangles' )
80101
81102 # Handling of linewidths, shading, edgecolors and antialiased as
82103 # in Axes.pcolor
@@ -97,13 +118,11 @@ def tripcolor(ax, *args, alpha=1.0, norm=None, cmap=None, vmin=None,
97118
98119 if shading == 'gouraud' :
99120 if facecolors is not None :
100- raise ValueError ('Gouraud shading does not support the use '
101- 'of facecolors kwarg' )
102- if len (C ) != len (tri .x ):
103- raise ValueError ('For gouraud shading, the length of color '
104- 'values array must be the same as the '
105- 'number of triangulation points' )
121+ raise ValueError (
122+ "shading='gouraud' can only be used when the colors "
123+ "are specified at the points, not at the faces." )
106124 collection = TriMesh (tri , ** kwargs )
125+ colors = point_colors
107126 else :
108127 # Vertices of triangles.
109128 maskedTris = tri .get_masked_triangles ()
@@ -112,15 +131,17 @@ def tripcolor(ax, *args, alpha=1.0, norm=None, cmap=None, vmin=None,
112131 # Color values.
113132 if facecolors is None :
114133 # One color per triangle, the mean of the 3 vertex color values.
115- C = C [maskedTris ].mean (axis = 1 )
134+ colors = point_colors [maskedTris ].mean (axis = 1 )
116135 elif tri .mask is not None :
117136 # Remove color values of masked triangles.
118- C = C [~ tri .mask ]
137+ colors = facecolors [~ tri .mask ]
138+ else :
139+ colors = facecolors
119140
120141 collection = PolyCollection (verts , ** kwargs )
121142
122143 collection .set_alpha (alpha )
123- collection .set_array (C )
144+ collection .set_array (colors )
124145 _api .check_isinstance ((Normalize , None ), norm = norm )
125146 collection .set_cmap (cmap )
126147 collection .set_norm (norm )
0 commit comments