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 

2from matplotlib.tri.triangulation import Triangulation 

3 

4 

5def triplot(ax, *args, **kwargs): 

6 """ 

7 Draw a unstructured triangular grid as lines and/or markers. 

8 

9 The triangulation to plot can be specified in one of two ways; 

10 either:: 

11 

12 triplot(triangulation, ...) 

13 

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

15 object, or 

16 

17 :: 

18 

19 triplot(x, y, ...) 

20 triplot(x, y, triangles, ...) 

21 triplot(x, y, triangles=triangles, ...) 

22 triplot(x, y, mask=mask, ...) 

23 triplot(x, y, triangles, mask=mask, ...) 

24 

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

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

27 possibilities. 

28 

29 The remaining args and kwargs are the same as for 

30 :meth:`~matplotlib.axes.Axes.plot`. 

31 

32 Return a list of 2 :class:`~matplotlib.lines.Line2D` containing 

33 respectively: 

34 

35 - the lines plotted for triangles edges 

36 - the markers plotted for triangles nodes 

37 """ 

38 import matplotlib.axes 

39 

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

41 x, y, edges = (tri.x, tri.y, tri.edges) 

42 

43 # Decode plot format string, e.g., 'ro-' 

44 fmt = args[0] if args else "" 

45 linestyle, marker, color = matplotlib.axes._base._process_plot_format(fmt) 

46 

47 # Insert plot format string into a copy of kwargs (kwargs values prevail). 

48 kw = kwargs.copy() 

49 for key, val in zip(('linestyle', 'marker', 'color'), 

50 (linestyle, marker, color)): 

51 if val is not None: 

52 kw[key] = kwargs.get(key, val) 

53 

54 # Draw lines without markers. 

55 # Note 1: If we drew markers here, most markers would be drawn more than 

56 # once as they belong to several edges. 

57 # Note 2: We insert nan values in the flattened edges arrays rather than 

58 # plotting directly (triang.x[edges].T, triang.y[edges].T) 

59 # as it considerably speeds-up code execution. 

60 linestyle = kw['linestyle'] 

61 kw_lines = { 

62 **kw, 

63 'marker': 'None', # No marker to draw. 

64 'zorder': kw.get('zorder', 1), # Path default zorder is used. 

65 } 

66 if linestyle not in [None, 'None', '', ' ']: 

67 tri_lines_x = np.insert(x[edges], 2, np.nan, axis=1) 

68 tri_lines_y = np.insert(y[edges], 2, np.nan, axis=1) 

69 tri_lines = ax.plot(tri_lines_x.ravel(), tri_lines_y.ravel(), 

70 **kw_lines) 

71 else: 

72 tri_lines = ax.plot([], [], **kw_lines) 

73 

74 # Draw markers separately. 

75 marker = kw['marker'] 

76 kw_markers = { 

77 **kw, 

78 'linestyle': 'None', # No line to draw. 

79 } 

80 if marker not in [None, 'None', '', ' ']: 

81 tri_markers = ax.plot(x, y, **kw_markers) 

82 else: 

83 tri_markers = ax.plot([], [], **kw_markers) 

84 

85 return tri_lines + tri_markers