|
3 | 3 |
|
4 | 4 | import six
|
5 | 5 |
|
6 |
| -from matplotlib.cbook import ls_mapper |
7 |
| -from matplotlib.patches import PathPatch |
8 |
| -from matplotlib.path import Path |
9 |
| -from matplotlib.tri.triangulation import Triangulation |
10 | 6 | import numpy as np
|
| 7 | +from matplotlib.tri.triangulation import Triangulation |
11 | 8 |
|
12 | 9 |
|
13 | 10 | def triplot(ax, *args, **kwargs):
|
@@ -37,46 +34,59 @@ def triplot(ax, *args, **kwargs):
|
37 | 34 | The remaining args and kwargs are the same as for
|
38 | 35 | :meth:`~matplotlib.axes.Axes.plot`.
|
39 | 36 |
|
| 37 | + Return a list of 2 :class:`~matplotlib.lines.Line2D` containing |
| 38 | + respectively: |
| 39 | +
|
| 40 | + - the lines plotted for triangles edges |
| 41 | + - the markers plotted for triangles nodes |
| 42 | +
|
40 | 43 | **Example:**
|
41 | 44 |
|
42 | 45 | .. plot:: mpl_examples/pylab_examples/triplot_demo.py
|
43 | 46 | """
|
44 | 47 | import matplotlib.axes
|
45 | 48 |
|
46 | 49 | tri, args, kwargs = Triangulation.get_from_args_and_kwargs(*args, **kwargs)
|
47 |
| - |
48 |
| - x = tri.x |
49 |
| - y = tri.y |
50 |
| - edges = tri.edges |
51 |
| - |
52 |
| - # If draw both lines and markers at the same time, e.g. |
53 |
| - # ax.plot(x[edges].T, y[edges].T, *args, **kwargs) |
54 |
| - # then the markers are drawn more than once which is incorrect if alpha<1. |
55 |
| - # Hence draw lines and markers separately. |
| 50 | + x, y, edges = (tri.x, tri.y, tri.edges) |
56 | 51 |
|
57 | 52 | # Decode plot format string, e.g., 'ro-'
|
58 |
| - fmt = '' |
| 53 | + fmt = "" |
59 | 54 | if len(args) > 0:
|
60 | 55 | fmt = args[0]
|
61 | 56 | linestyle, marker, color = matplotlib.axes._base._process_plot_format(fmt)
|
62 | 57 |
|
63 |
| - # Draw lines without markers, if lines are required. |
64 |
| - if linestyle is not None and linestyle is not 'None': |
65 |
| - kw = kwargs.copy() |
66 |
| - kw.pop('marker', None) # Ignore marker if set. |
67 |
| - kw['linestyle'] = ls_mapper[linestyle] |
68 |
| - kw['edgecolor'] = color |
69 |
| - kw['facecolor'] = None |
70 |
| - |
71 |
| - vertices = np.column_stack((x[edges].flatten(), y[edges].flatten())) |
72 |
| - codes = ([Path.MOVETO] + [Path.LINETO])*len(edges) |
73 |
| - |
74 |
| - path = Path(vertices, codes) |
75 |
| - pathpatch = PathPatch(path, **kw) |
76 |
| - |
77 |
| - ax.add_patch(pathpatch) |
78 |
| - |
79 |
| - # Draw markers without lines. |
80 |
| - # Should avoid drawing markers for points that are not in any triangle? |
81 |
| - kwargs['linestyle'] = '' |
82 |
| - ax.plot(x, y, *args, **kwargs) |
| 58 | + # Insert plot format string into a copy of kwargs (kwargs values prevail). |
| 59 | + kw = kwargs.copy() |
| 60 | + for key, val in zip(('linestyle', 'marker', 'color'), |
| 61 | + (linestyle, marker, color)): |
| 62 | + if val is not None: |
| 63 | + kw[key] = kwargs.get(key, val) |
| 64 | + |
| 65 | + # Draw lines without markers. |
| 66 | + # Note 1: If we drew markers here, most markers would be drawn more than |
| 67 | + # once as they belong to several edges. |
| 68 | + # Note 2: We insert nan values in the flattened edges arrays rather than |
| 69 | + # plotting directly (triang.x[edges].T, triang.y[edges].T) |
| 70 | + # as it considerably speeds-up code execution. |
| 71 | + linestyle = kw['linestyle'] |
| 72 | + kw_lines = kw.copy() |
| 73 | + kw_lines['marker'] = 'None' # No marker to draw. |
| 74 | + kw_lines['zorder'] = kw.get('zorder', 1) # Path default zorder is used. |
| 75 | + if (linestyle is not None) and (linestyle not in ['None', '', ' ']): |
| 76 | + tri_lines_x = np.insert(x[edges], 2, np.nan, axis=1) |
| 77 | + tri_lines_y = np.insert(y[edges], 2, np.nan, axis=1) |
| 78 | + tri_lines = ax.plot(tri_lines_x.ravel(), tri_lines_y.ravel(), |
| 79 | + **kw_lines) |
| 80 | + else: |
| 81 | + tri_lines = ax.plot([], [], **kw_lines) |
| 82 | + |
| 83 | + # Draw markers separately. |
| 84 | + marker = kw['marker'] |
| 85 | + kw_markers = kw.copy() |
| 86 | + kw_markers['linestyle'] = 'None' # No line to draw. |
| 87 | + if (marker is not None) and (marker not in ['None', '', ' ']): |
| 88 | + tri_markers = ax.plot(x, y, **kw_markers) |
| 89 | + else: |
| 90 | + tri_markers = ax.plot([], [], **kw_markers) |
| 91 | + |
| 92 | + return tri_lines + tri_markers |
0 commit comments