Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1import numpy as np 

2 

3from matplotlib import cbook 

4from matplotlib.collections import PolyCollection, TriMesh 

5from matplotlib.colors import Normalize 

6from matplotlib.tri.triangulation import Triangulation 

7 

8 

9def tripcolor(ax, *args, alpha=1.0, norm=None, cmap=None, vmin=None, 

10 vmax=None, shading='flat', facecolors=None, **kwargs): 

11 """ 

12 Create a pseudocolor plot of an unstructured triangular grid. 

13 

14 The triangulation can be specified in one of two ways; either:: 

15 

16 tripcolor(triangulation, ...) 

17 

18 where triangulation is a :class:`matplotlib.tri.Triangulation` 

19 object, or 

20 

21 :: 

22 

23 tripcolor(x, y, ...) 

24 tripcolor(x, y, triangles, ...) 

25 tripcolor(x, y, triangles=triangles, ...) 

26 tripcolor(x, y, mask=mask, ...) 

27 tripcolor(x, y, triangles, mask=mask, ...) 

28 

29 in which case a Triangulation object will be created. See 

30 :class:`~matplotlib.tri.Triangulation` for a explanation of these 

31 possibilities. 

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 The remaining kwargs are the same as for 

48 :meth:`~matplotlib.axes.Axes.pcolor`. 

49 """ 

50 cbook._check_in_list(['flat', 'gouraud'], shading=shading) 

51 

52 tri, args, kwargs = Triangulation.get_from_args_and_kwargs(*args, **kwargs) 

53 

54 # C is the colors array defined at either points or faces (i.e. triangles). 

55 # If facecolors is None, C are defined at points. 

56 # If facecolors is not None, C are defined at faces. 

57 if facecolors is not None: 

58 C = facecolors 

59 else: 

60 C = np.asarray(args[0]) 

61 

62 # If there are a different number of points and triangles in the 

63 # triangulation, can omit facecolors kwarg as it is obvious from 

64 # length of C whether it refers to points or faces. 

65 # Do not do this for gouraud shading. 

66 if (facecolors is None and len(C) == len(tri.triangles) and 

67 len(C) != len(tri.x) and shading != 'gouraud'): 

68 facecolors = C 

69 

70 # Check length of C is OK. 

71 if ((facecolors is None and len(C) != len(tri.x)) or 

72 (facecolors is not None and len(C) != len(tri.triangles))): 

73 raise ValueError('Length of color values array must be the same ' 

74 'as either the number of triangulation points ' 

75 'or triangles') 

76 

77 # Handling of linewidths, shading, edgecolors and antialiased as 

78 # in Axes.pcolor 

79 linewidths = (0.25,) 

80 if 'linewidth' in kwargs: 

81 kwargs['linewidths'] = kwargs.pop('linewidth') 

82 kwargs.setdefault('linewidths', linewidths) 

83 

84 edgecolors = 'none' 

85 if 'edgecolor' in kwargs: 

86 kwargs['edgecolors'] = kwargs.pop('edgecolor') 

87 ec = kwargs.setdefault('edgecolors', edgecolors) 

88 

89 if 'antialiased' in kwargs: 

90 kwargs['antialiaseds'] = kwargs.pop('antialiased') 

91 if 'antialiaseds' not in kwargs and ec.lower() == "none": 

92 kwargs['antialiaseds'] = False 

93 

94 if shading == 'gouraud': 

95 if facecolors is not None: 

96 raise ValueError('Gouraud shading does not support the use ' 

97 'of facecolors kwarg') 

98 if len(C) != len(tri.x): 

99 raise ValueError('For gouraud shading, the length of color ' 

100 'values array must be the same as the ' 

101 'number of triangulation points') 

102 collection = TriMesh(tri, **kwargs) 

103 else: 

104 # Vertices of triangles. 

105 maskedTris = tri.get_masked_triangles() 

106 verts = np.stack((tri.x[maskedTris], tri.y[maskedTris]), axis=-1) 

107 

108 # Color values. 

109 if facecolors is None: 

110 # One color per triangle, the mean of the 3 vertex color values. 

111 C = C[maskedTris].mean(axis=1) 

112 elif tri.mask is not None: 

113 # Remove color values of masked triangles. 

114 C = C[~tri.mask] 

115 

116 collection = PolyCollection(verts, **kwargs) 

117 

118 collection.set_alpha(alpha) 

119 collection.set_array(C) 

120 cbook._check_isinstance((Normalize, None), norm=norm) 

121 collection.set_cmap(cmap) 

122 collection.set_norm(norm) 

123 if vmin is not None or vmax is not None: 

124 collection.set_clim(vmin, vmax) 

125 else: 

126 collection.autoscale_None() 

127 ax.grid(False) 

128 

129 minx = tri.x.min() 

130 maxx = tri.x.max() 

131 miny = tri.y.min() 

132 maxy = tri.y.max() 

133 corners = (minx, miny), (maxx, maxy) 

134 ax.update_datalim(corners) 

135 ax.autoscale_view() 

136 ax.add_collection(collection) 

137 return collection