Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7c7b037

Browse files
committed
Added tripcolor triangle-centred colour values.
1 parent 267b0cf commit 7c7b037

File tree

1 file changed

+40
-17
lines changed

1 file changed

+40
-17
lines changed

lib/matplotlib/tri/tripcolor.py

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,19 @@ def tripcolor(ax, *args, **kwargs):
2828
:class:`~matplotlib.tri.Triangulation` for a explanation of these
2929
possibilities.
3030
31-
The next argument must be *C*, the array of color values, one per
32-
point in the triangulation.
33-
34-
*shading* may be 'flat', 'faceted' or 'gouraud'. If *shading* is
35-
'flat' or 'faceted', the colors used for each triangle are from
36-
the mean C of the triangle's three points.
31+
The next argument must be *C*, the array of color values, either
32+
one per point in the triangulation if color values are defined at
33+
points, or one per triangle in the triangulation if color values
34+
are defined at triangles. If there are the same number of points
35+
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*.
38+
39+
*shading* may be 'flat' (the default), 'faceted' or 'gouraud'. If
40+
*shading* is 'flat' or 'faceted' and C values are defined at
41+
points, the color values used for each triangle are from the mean
42+
C of the triangle's three points. If *shading* is 'gouraud' then
43+
color values must be defined at triangles.
3744
3845
The remaining kwargs are the same as for
3946
:meth:`~matplotlib.axes.Axes.pcolor`.
@@ -50,20 +57,28 @@ def tripcolor(ax, *args, **kwargs):
5057
vmin = kwargs.pop('vmin', None)
5158
vmax = kwargs.pop('vmax', None)
5259
shading = kwargs.pop('shading', 'flat')
60+
colorpoints = kwargs.pop('colorpoints', True)
5361

5462
tri, args, kwargs = Triangulation.get_from_args_and_kwargs(*args, **kwargs)
55-
x = tri.x
56-
y = tri.y
57-
triangles = tri.get_masked_triangles()
58-
5963
C = np.asarray(args[0])
60-
if C.shape != x.shape:
61-
raise ValueError('C array must have same length as triangulation x and'
62-
' y arrays')
6364

6465
if shading == 'gouraud':
66+
if len(C) != len(tri.x):
67+
raise ValueError('For gouraud shading, the length of C '
68+
'array must be the same as the number of '
69+
'triangulation points')
6570
collection = TriMesh(tri, **kwargs)
6671
else:
72+
if len(C) != len(tri.x) and len(C) != len(tri.triangles):
73+
raise ValueError('Length of C array must be the same as either '
74+
'the number of triangulation points or triangles')
75+
76+
# CAtPoints is True if C defined at points
77+
# or False if C defined at triangles.
78+
CAtPoints = (len(C) == len(tri.x))
79+
if len(C) == len(tri.x) and len(C) == len(tri.triangles):
80+
CAtPoints = colorpoints
81+
6782
if shading == 'faceted':
6883
edgecolors = (0,0,0,1),
6984
linewidths = (0.25,)
@@ -75,10 +90,18 @@ def tripcolor(ax, *args, **kwargs):
7590
kwargs.setdefault('linewidths', linewidths)
7691

7792
# Vertices of triangles.
78-
verts = np.concatenate((x[triangles][...,np.newaxis],
79-
y[triangles][...,np.newaxis]), axis=2)
80-
# Color values, one per triangle, mean of the 3 vertex color values.
81-
C = C[triangles].mean(axis=1)
93+
maskedTris = tri.get_masked_triangles()
94+
verts = np.concatenate((tri.x[maskedTris][...,np.newaxis],
95+
tri.y[maskedTris][...,np.newaxis]), axis=2)
96+
97+
# Color values.
98+
if CAtPoints:
99+
# One color per triangle, the mean of the 3 vertex color values.
100+
C = C[maskedTris].mean(axis=1)
101+
elif tri.mask is not None:
102+
# Remove color values of masked triangles.
103+
C = C.compress(1-tri.mask)
104+
82105
collection = PolyCollection(verts, **kwargs)
83106

84107
collection.set_alpha(alpha)

0 commit comments

Comments
 (0)