@@ -21,62 +21,83 @@ 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
35
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.
53
63
"""
54
64
_api .check_in_list (['flat' , 'gouraud' ], shading = shading )
55
65
56
66
tri , args , kwargs = Triangulation .get_from_args_and_kwargs (* args , ** kwargs )
57
67
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
61
71
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" )
63
80
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" )
64
89
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' )
80
101
81
102
# Handling of linewidths, shading, edgecolors and antialiased as
82
103
# in Axes.pcolor
@@ -97,13 +118,11 @@ def tripcolor(ax, *args, alpha=1.0, norm=None, cmap=None, vmin=None,
97
118
98
119
if shading == 'gouraud' :
99
120
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." )
106
124
collection = TriMesh (tri , ** kwargs )
125
+ colors = point_colors
107
126
else :
108
127
# Vertices of triangles.
109
128
maskedTris = tri .get_masked_triangles ()
@@ -112,15 +131,17 @@ def tripcolor(ax, *args, alpha=1.0, norm=None, cmap=None, vmin=None,
112
131
# Color values.
113
132
if facecolors is None :
114
133
# 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 )
116
135
elif tri .mask is not None :
117
136
# Remove color values of masked triangles.
118
- C = C [~ tri .mask ]
137
+ colors = facecolors [~ tri .mask ]
138
+ else :
139
+ colors = facecolors
119
140
120
141
collection = PolyCollection (verts , ** kwargs )
121
142
122
143
collection .set_alpha (alpha )
123
- collection .set_array (C )
144
+ collection .set_array (colors )
124
145
_api .check_isinstance ((Normalize , None ), norm = norm )
125
146
collection .set_cmap (cmap )
126
147
collection .set_norm (norm )
0 commit comments