Skip to content

Commit a4d4031

Browse files
committed
Merge pull request #2781 from GBillotey/return_triplot
Triplot returns the artist it adds.
2 parents 96bf747 + 69e0be3 commit a4d4031

File tree

4 files changed

+61
-37
lines changed

4 files changed

+61
-37
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2014-01-02 `triplot` now returns the artist it adds and support of line and
2+
marker kwargs has been improved. GBY
3+
14
2013-12-30 Made streamplot grid size consistent for different types of density
25
argument. A 30x30 grid is now used for both density=1 and
36
density=(1, 1).

lib/matplotlib/axes/_axes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6719,5 +6719,5 @@ def tripcolor(self, *args, **kwargs):
67196719
tripcolor.__doc__ = mtri.tripcolor.__doc__
67206720

67216721
def triplot(self, *args, **kwargs):
6722-
mtri.triplot(self, *args, **kwargs)
6722+
return mtri.triplot(self, *args, **kwargs)
67236723
triplot.__doc__ = mtri.triplot.__doc__

lib/matplotlib/tests/test_triangulation.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -906,9 +906,9 @@ def test_trirefine():
906906
# Testing the mask of the refined triangulation
907907
refi_mask = refi_triang.mask
908908
refi_tri_barycenter_x = np.sum(refi_triang.x[refi_triang.triangles],
909-
axis=1)/3.
909+
axis=1) / 3.
910910
refi_tri_barycenter_y = np.sum(refi_triang.y[refi_triang.triangles],
911-
axis=1)/3.
911+
axis=1) / 3.
912912
tri_finder = triang.get_trifinder()
913913
refi_tri_indices = tri_finder(refi_tri_barycenter_x,
914914
refi_tri_barycenter_y)
@@ -949,6 +949,17 @@ def meshgrid_triangles(n):
949949
return np.array(tri, dtype=np.int32)
950950

951951

952+
def test_triplot_return():
953+
# Check that triplot returns the artists it adds
954+
from matplotlib.figure import Figure
955+
ax = Figure().add_axes([0.1, 0.1, 0.7, 0.7])
956+
triang = mtri.Triangulation(
957+
[0.0, 1.0, 0.0, 1.0], [0.0, 0.0, 1.0, 1.0],
958+
triangles=[[0, 1, 3], [3, 2, 0]])
959+
if ax.triplot(triang, "b-") is None:
960+
raise AssertionError("triplot should return the artist it adds")
961+
962+
952963
if __name__ == '__main__':
953964
import nose
954965
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

lib/matplotlib/tri/triplot.py

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33

44
import six
55

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
106
import numpy as np
7+
from matplotlib.tri.triangulation import Triangulation
118

129

1310
def triplot(ax, *args, **kwargs):
@@ -37,46 +34,59 @@ def triplot(ax, *args, **kwargs):
3734
The remaining args and kwargs are the same as for
3835
:meth:`~matplotlib.axes.Axes.plot`.
3936
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+
4043
**Example:**
4144
4245
.. plot:: mpl_examples/pylab_examples/triplot_demo.py
4346
"""
4447
import matplotlib.axes
4548

4649
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)
5651

5752
# Decode plot format string, e.g., 'ro-'
58-
fmt = ''
53+
fmt = ""
5954
if len(args) > 0:
6055
fmt = args[0]
6156
linestyle, marker, color = matplotlib.axes._base._process_plot_format(fmt)
6257

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

Comments
 (0)