@@ -21,62 +21,85 @@ 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. using the call
61
+ signature ``tripcolor(x, y, triangles, C, [mask=mask], ...)``. However,
62
+ this is discouraged. For more clarity, pass *triangles* via keyword
63
+ argument.
53
64
"""
54
65
_api .check_in_list (['flat' , 'gouraud' ], shading = shading )
55
66
56
67
tri , args , kwargs = Triangulation .get_from_args_and_kwargs (* args , ** kwargs )
57
68
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.
69
+ # Parse the color to be in one of (the other variable will be None):
70
+ # - facecolors: if specified at the triangle faces
71
+ # - node_colors: if specified at the points
61
72
if facecolors is not None :
62
- C = facecolors
73
+ print (args )
74
+ if args :
75
+ _api .warn_external (
76
+ "Positional parameter C has no effect when the keyword "
77
+ "facecolors is given" )
78
+ node_colors = None
79
+ if len (facecolors ) != len (tri .triangles ):
80
+ raise ValueError ("The length of facecolors must match the number "
81
+ "of triangles" )
63
82
else :
83
+ # Color from positional parameter C
84
+ if not args :
85
+ raise ValueError (
86
+ "Missing color parameter. Please pass C positionally or "
87
+ "facecolors via keyword" )
88
+ elif len (args ) > 1 :
89
+ _api .warn_external (
90
+ "Additional positional parameters {args[1:]!r} are ignored" )
64
91
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' )
92
+ if len (C ) == len (tri .x ):
93
+ # having this before the len(tri.triangles) comparison gives
94
+ # precedence to nodes if there are as many nodes as triangles
95
+ node_colors = C
96
+ facecolors = None
97
+ elif len (C ) == len (tri .triangles ):
98
+ node_colors = None
99
+ facecolors = C
100
+ else :
101
+ raise ValueError ('The length of C must match either the number '
102
+ 'of points or the number of triangles' )
80
103
81
104
# Handling of linewidths, shading, edgecolors and antialiased as
82
105
# in Axes.pcolor
@@ -97,13 +120,11 @@ def tripcolor(ax, *args, alpha=1.0, norm=None, cmap=None, vmin=None,
97
120
98
121
if shading == 'gouraud' :
99
122
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' )
123
+ raise ValueError (
124
+ "shading='gouraud' can only be used when the colors "
125
+ "are specified at the points, not at the faces." )
106
126
collection = TriMesh (tri , ** kwargs )
127
+ colors = node_colors
107
128
else :
108
129
# Vertices of triangles.
109
130
maskedTris = tri .get_masked_triangles ()
@@ -112,15 +133,17 @@ def tripcolor(ax, *args, alpha=1.0, norm=None, cmap=None, vmin=None,
112
133
# Color values.
113
134
if facecolors is None :
114
135
# One color per triangle, the mean of the 3 vertex color values.
115
- C = C [maskedTris ].mean (axis = 1 )
136
+ colors = node_colors [maskedTris ].mean (axis = 1 )
116
137
elif tri .mask is not None :
117
138
# Remove color values of masked triangles.
118
- C = C [~ tri .mask ]
139
+ colors = facecolors [~ tri .mask ]
140
+ else :
141
+ colors = facecolors
119
142
120
143
collection = PolyCollection (verts , ** kwargs )
121
144
122
145
collection .set_alpha (alpha )
123
- collection .set_array (C )
146
+ collection .set_array (colors )
124
147
_api .check_isinstance ((Normalize , None ), norm = norm )
125
148
collection .set_cmap (cmap )
126
149
collection .set_norm (norm )
0 commit comments