_triplot.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import numpy as np
  2. from matplotlib.tri._triangulation import Triangulation
  3. import matplotlib.cbook as cbook
  4. import matplotlib.lines as mlines
  5. def triplot(ax, *args, **kwargs):
  6. """
  7. Draw an unstructured triangular grid as lines and/or markers.
  8. Call signatures::
  9. triplot(triangulation, ...)
  10. triplot(x, y, [triangles], *, [mask=mask], ...)
  11. The triangular grid can be specified either by passing a `.Triangulation`
  12. object as the first parameter, or by passing the points *x*, *y* and
  13. optionally the *triangles* and a *mask*. If neither of *triangulation* or
  14. *triangles* are given, the triangulation is calculated on the fly.
  15. Parameters
  16. ----------
  17. triangulation : `.Triangulation`
  18. An already created triangular grid.
  19. x, y, triangles, mask
  20. Parameters defining the triangular grid. See `.Triangulation`.
  21. This is mutually exclusive with specifying *triangulation*.
  22. other_parameters
  23. All other args and kwargs are forwarded to `~.Axes.plot`.
  24. Returns
  25. -------
  26. lines : `~matplotlib.lines.Line2D`
  27. The drawn triangles edges.
  28. markers : `~matplotlib.lines.Line2D`
  29. The drawn marker nodes.
  30. """
  31. import matplotlib.axes
  32. tri, args, kwargs = Triangulation.get_from_args_and_kwargs(*args, **kwargs)
  33. x, y, edges = (tri.x, tri.y, tri.edges)
  34. # Decode plot format string, e.g., 'ro-'
  35. fmt = args[0] if args else ""
  36. linestyle, marker, color = matplotlib.axes._base._process_plot_format(fmt)
  37. # Insert plot format string into a copy of kwargs (kwargs values prevail).
  38. kw = cbook.normalize_kwargs(kwargs, mlines.Line2D)
  39. for key, val in zip(('linestyle', 'marker', 'color'),
  40. (linestyle, marker, color)):
  41. if val is not None:
  42. kw.setdefault(key, val)
  43. # Draw lines without markers.
  44. # Note 1: If we drew markers here, most markers would be drawn more than
  45. # once as they belong to several edges.
  46. # Note 2: We insert nan values in the flattened edges arrays rather than
  47. # plotting directly (triang.x[edges].T, triang.y[edges].T)
  48. # as it considerably speeds-up code execution.
  49. linestyle = kw['linestyle']
  50. kw_lines = {
  51. **kw,
  52. 'marker': 'None', # No marker to draw.
  53. 'zorder': kw.get('zorder', 1), # Path default zorder is used.
  54. }
  55. if linestyle not in [None, 'None', '', ' ']:
  56. tri_lines_x = np.insert(x[edges], 2, np.nan, axis=1)
  57. tri_lines_y = np.insert(y[edges], 2, np.nan, axis=1)
  58. tri_lines = ax.plot(tri_lines_x.ravel(), tri_lines_y.ravel(),
  59. **kw_lines)
  60. else:
  61. tri_lines = ax.plot([], [], **kw_lines)
  62. # Draw markers separately.
  63. marker = kw['marker']
  64. kw_markers = {
  65. **kw,
  66. 'linestyle': 'None', # No line to draw.
  67. }
  68. kw_markers.pop('label', None)
  69. if marker not in [None, 'None', '', ' ']:
  70. tri_markers = ax.plot(x, y, **kw_markers)
  71. else:
  72. tri_markers = ax.plot([], [], **kw_markers)
  73. return tri_lines + tri_markers