@@ -21,62 +21,82 @@ def tripcolor(ax, *args, alpha=1.0, norm=None, cmap=None, vmin=None,
21
21
optionally the *triangles* and a *mask*. See `.Triangulation` for an
22
22
explanation of these parameters.
23
23
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
+
24
30
Parameters
25
31
----------
26
32
triangulation : `.Triangulation`
27
33
An already created triangular grid.
28
34
x, y, triangles, mask
29
- 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`.
35
+ Parameters defining the triangular grid. See `.Triangulation`.
36
+ This is mutually exclusive with specifying *triangulation*.
37
+ C : array-like
38
+ The color values, either for the points or for the triangles. Which one
39
+ is automatically inferred from the length of *C*, i.e. does it match
40
+ the number of points or the number of triangles. If there are the same
41
+ number of points and triangles in the triangulation it is assumed that
42
+ color values are defined at points; to force the use of color values at
43
+ triangles use the keyword argument ``facecolors=C`` instead of just
44
+ ``C``.
45
+ This parameter is position-only.
46
+ facecolors : array-like, optional
47
+ Can be used alternatively to *C* to specify colors at the triangle
48
+ faces. This parameter takes precedence over *C*.
49
+ shading : {'flat', 'gouraud'}, default: 'flat'
50
+ If 'flat' and the color values *C* are defined at points, the color
51
+ values used for each triangle are from the mean C of the triangle's
52
+ three points. If *shading* is 'gouraud' then color values must be
53
+ defined at points.
54
+ other_parameters
55
+ All other parameters are the same as for `~.Axes.pcolor`.
56
+
57
+ Notes
58
+ -----
59
+ It is possible to pass the triangles positionally, i.e.
60
+ ``tripcolor(x, y, triangles, C, ...)``. However, this is discouraged.
61
+ For more clarity, pass *triangles* via keyword argument.
53
62
"""
54
63
_api .check_in_list (['flat' , 'gouraud' ], shading = shading )
55
64
56
65
tri , args , kwargs = Triangulation .get_from_args_and_kwargs (* args , ** kwargs )
57
66
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.
67
+ # Parse the color to be in one of (the other variable will be None):
68
+ # - facecolors: if specified at the triangle faces
69
+ # - point_colors: if specified at the points
61
70
if facecolors is not None :
62
- C = facecolors
71
+ if args :
72
+ _api .warn_external (
73
+ "Positional parameter C has no effect when the keyword "
74
+ "facecolors is given" )
75
+ point_colors = None
76
+ if len (facecolors ) != len (tri .triangles ):
77
+ raise ValueError ("The length of facecolors must match the number "
78
+ "of triangles" )
63
79
else :
80
+ # Color from positional parameter C
81
+ if not args :
82
+ raise ValueError (
83
+ "Missing color parameter. Please pass C positionally or "
84
+ "facecolors via keyword" )
85
+ elif len (args ) > 1 :
86
+ _api .warn_external (
87
+ "Additional positional parameters {args[1:]!r} are ignored" )
64
88
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' )
89
+ if len (C ) == len (tri .x ):
90
+ # having this before the len(tri.triangles) comparison gives
91
+ # precedence to nodes if there are as many nodes as triangles
92
+ point_colors = C
93
+ facecolors = None
94
+ elif len (C ) == len (tri .triangles ):
95
+ point_colors = None
96
+ facecolors = C
97
+ else :
98
+ raise ValueError ('The length of C must match either the number '
99
+ 'of points or the number of triangles' )
80
100
81
101
# Handling of linewidths, shading, edgecolors and antialiased as
82
102
# in Axes.pcolor
@@ -97,13 +117,11 @@ def tripcolor(ax, *args, alpha=1.0, norm=None, cmap=None, vmin=None,
97
117
98
118
if shading == 'gouraud' :
99
119
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' )
120
+ raise ValueError (
121
+ "shading='gouraud' can only be used when the colors "
122
+ "are specified at the points, not at the faces." )
106
123
collection = TriMesh (tri , ** kwargs )
124
+ colors = point_colors
107
125
else :
108
126
# Vertices of triangles.
109
127
maskedTris = tri .get_masked_triangles ()
@@ -112,15 +130,17 @@ def tripcolor(ax, *args, alpha=1.0, norm=None, cmap=None, vmin=None,
112
130
# Color values.
113
131
if facecolors is None :
114
132
# One color per triangle, the mean of the 3 vertex color values.
115
- C = C [maskedTris ].mean (axis = 1 )
133
+ colors = point_colors [maskedTris ].mean (axis = 1 )
116
134
elif tri .mask is not None :
117
135
# Remove color values of masked triangles.
118
- C = C [~ tri .mask ]
136
+ colors = facecolors [~ tri .mask ]
137
+ else :
138
+ colors = facecolors
119
139
120
140
collection = PolyCollection (verts , ** kwargs )
121
141
122
142
collection .set_alpha (alpha )
123
- collection .set_array (C )
143
+ collection .set_array (colors )
124
144
_api .check_isinstance ((Normalize , None ), norm = norm )
125
145
collection .set_cmap (cmap )
126
146
collection .set_norm (norm )
0 commit comments