/matplotlib-1.0.1/lib/matplotlib/tri/tripcolor.py

# · Python · 100 lines · 66 code · 4 blank · 30 comment · 0 complexity · eea77e4a09cb11cbf93ebda9fcb0a775 MD5 · raw file

  1. from matplotlib.collections import PolyCollection
  2. from matplotlib.colors import Normalize
  3. from matplotlib.tri.triangulation import Triangulation
  4. import numpy as np
  5. def tripcolor(ax, *args, **kwargs):
  6. """
  7. Create a pseudocolor plot of an unstructured triangular grid to
  8. the :class:`~matplotlib.axes.Axes`.
  9. The triangulation can be specified in one of two ways; either::
  10. tripcolor(triangulation, ...)
  11. where triangulation is a :class:`~matplotlib.tri.Triangulation`
  12. object, or
  13. ::
  14. tripcolor(x, y, ...)
  15. tripcolor(x, y, triangles, ...)
  16. tripcolor(x, y, triangles=triangles, ...)
  17. tripcolor(x, y, mask, ...)
  18. tripcolor(x, y, mask=mask, ...)
  19. tripcolor(x, y, triangles, mask, ...)
  20. tripcolor(x, y, triangles, mask=mask, ...)
  21. in which case a Triangulation object will be created. See
  22. :class:`~matplotlib.tri.Triangulation` for a explanation of these
  23. possibilities.
  24. The next argument must be *C*, the array of color values, one per
  25. point in the triangulation. The colors used for each triangle
  26. are from the mean C of the triangle's three points.
  27. The remaining kwargs are the same as for
  28. :meth:`~matplotlib.axes.Axes.pcolor`.
  29. **Example:**
  30. .. plot:: mpl_examples/pylab_examples/tripcolor_demo.py
  31. """
  32. if not ax._hold: ax.cla()
  33. alpha = kwargs.pop('alpha', 1.0)
  34. norm = kwargs.pop('norm', None)
  35. cmap = kwargs.pop('cmap', None)
  36. vmin = kwargs.pop('vmin', None)
  37. vmax = kwargs.pop('vmax', None)
  38. shading = kwargs.pop('shading', 'flat')
  39. tri, args, kwargs = Triangulation.get_from_args_and_kwargs(*args, **kwargs)
  40. x = tri.x
  41. y = tri.y
  42. triangles = tri.get_masked_triangles()
  43. # Vertices of triangles.
  44. verts = np.concatenate((x[triangles][...,np.newaxis],
  45. y[triangles][...,np.newaxis]), axis=2)
  46. C = np.asarray(args[0])
  47. if C.shape != x.shape:
  48. raise ValueError('C array must have same length as triangulation x and'
  49. ' y arrays')
  50. # Color values, one per triangle, mean of the 3 vertex color values.
  51. C = C[triangles].mean(axis=1)
  52. if shading == 'faceted':
  53. edgecolors = (0,0,0,1),
  54. linewidths = (0.25,)
  55. else:
  56. edgecolors = 'face'
  57. linewidths = (1.0,)
  58. kwargs.setdefault('edgecolors', edgecolors)
  59. kwargs.setdefault('antialiaseds', (0,))
  60. kwargs.setdefault('linewidths', linewidths)
  61. collection = PolyCollection(verts, **kwargs)
  62. collection.set_alpha(alpha)
  63. collection.set_array(C)
  64. if norm is not None: assert(isinstance(norm, Normalize))
  65. collection.set_cmap(cmap)
  66. collection.set_norm(norm)
  67. if vmin is not None or vmax is not None:
  68. collection.set_clim(vmin, vmax)
  69. else:
  70. collection.autoscale_None()
  71. ax.grid(False)
  72. minx = tri.x.min()
  73. maxx = tri.x.max()
  74. miny = tri.y.min()
  75. maxy = tri.y.max()
  76. corners = (minx, miny), (maxx, maxy)
  77. ax.update_datalim( corners)
  78. ax.autoscale_view()
  79. ax.add_collection(collection)
  80. return collection