triplot.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import numpy as np
  2. from matplotlib.tri.triangulation import Triangulation
  3. def triplot(ax, *args, **kwargs):
  4. """
  5. Draw a unstructured triangular grid as lines and/or markers.
  6. The triangulation to plot can be specified in one of two ways;
  7. either::
  8. triplot(triangulation, ...)
  9. where triangulation is a :class:`matplotlib.tri.Triangulation`
  10. object, or
  11. ::
  12. triplot(x, y, ...)
  13. triplot(x, y, triangles, ...)
  14. triplot(x, y, triangles=triangles, ...)
  15. triplot(x, y, mask=mask, ...)
  16. triplot(x, y, triangles, mask=mask, ...)
  17. in which case a Triangulation object will be created. See
  18. :class:`~matplotlib.tri.Triangulation` for a explanation of these
  19. possibilities.
  20. The remaining args and kwargs are the same as for
  21. :meth:`~matplotlib.axes.Axes.plot`.
  22. Return a list of 2 :class:`~matplotlib.lines.Line2D` containing
  23. respectively:
  24. - the lines plotted for triangles edges
  25. - the markers plotted for triangles nodes
  26. """
  27. import matplotlib.axes
  28. tri, args, kwargs = Triangulation.get_from_args_and_kwargs(*args, **kwargs)
  29. x, y, edges = (tri.x, tri.y, tri.edges)
  30. # Decode plot format string, e.g., 'ro-'
  31. fmt = args[0] if args else ""
  32. linestyle, marker, color = matplotlib.axes._base._process_plot_format(fmt)
  33. # Insert plot format string into a copy of kwargs (kwargs values prevail).
  34. kw = kwargs.copy()
  35. for key, val in zip(('linestyle', 'marker', 'color'),
  36. (linestyle, marker, color)):
  37. if val is not None:
  38. kw[key] = kwargs.get(key, val)
  39. # Draw lines without markers.
  40. # Note 1: If we drew markers here, most markers would be drawn more than
  41. # once as they belong to several edges.
  42. # Note 2: We insert nan values in the flattened edges arrays rather than
  43. # plotting directly (triang.x[edges].T, triang.y[edges].T)
  44. # as it considerably speeds-up code execution.
  45. linestyle = kw['linestyle']
  46. kw_lines = {
  47. **kw,
  48. 'marker': 'None', # No marker to draw.
  49. 'zorder': kw.get('zorder', 1), # Path default zorder is used.
  50. }
  51. if linestyle not in [None, 'None', '', ' ']:
  52. tri_lines_x = np.insert(x[edges], 2, np.nan, axis=1)
  53. tri_lines_y = np.insert(y[edges], 2, np.nan, axis=1)
  54. tri_lines = ax.plot(tri_lines_x.ravel(), tri_lines_y.ravel(),
  55. **kw_lines)
  56. else:
  57. tri_lines = ax.plot([], [], **kw_lines)
  58. # Draw markers separately.
  59. marker = kw['marker']
  60. kw_markers = {
  61. **kw,
  62. 'linestyle': 'None', # No line to draw.
  63. }
  64. if marker not in [None, 'None', '', ' ']:
  65. tri_markers = ax.plot(x, y, **kw_markers)
  66. else:
  67. tri_markers = ax.plot([], [], **kw_markers)
  68. return tri_lines + tri_markers