Skip to content

Commit 9893d4f

Browse files
committed
FIX: pull the unscaled dashes from the line collection
To prevent double-scaling of the pattern in the legend Closes matplotlib#7814
1 parent 9fb4f9b commit 9893d4f

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

lib/matplotlib/legend_handler.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,12 +266,11 @@ def get_numpoints(self, legend):
266266

267267
def _default_update_prop(self, legend_handle, orig_handle):
268268
lw = orig_handle.get_linewidths()[0]
269-
dashes = orig_handle.get_dashes()[0]
269+
dashes = orig_handle._us_linestyles[0]
270270
color = orig_handle.get_colors()[0]
271271
legend_handle.set_color(color)
272+
legend_handle.set_linestyle(dashes)
272273
legend_handle.set_linewidth(lw)
273-
if dashes[0] is not None: # dashed line
274-
legend_handle.set_dashes(dashes[1])
275274

276275
def create_artists(self, legend, orig_handle,
277276
xdescent, ydescent, width, height, fontsize, trans):

lib/matplotlib/tests/test_legend.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import matplotlib as mpl
1818
import matplotlib.patches as mpatches
1919
import matplotlib.transforms as mtrans
20-
20+
import matplotlib.collections as mc
2121

2222
@image_comparison(baseline_images=['legend_auto1'], remove_text=True)
2323
def test_legend_auto1():
@@ -296,7 +296,28 @@ def test_not_covering_scatter_transform():
296296
plt.legend(['foo', 'bar'], loc='best')
297297

298298

299+
@cleanup
300+
def test_linecollection_scaled_dashes():
301+
lines1 = [[(0, .5), (.5, 1)], [(.3, .6), (.2, .2)]]
302+
lines2 = [[[0.7, .2], [.8, .4]], [[.5, .7], [.6, .1]]]
303+
lines3 = [[[0.6, .2], [.8, .4]], [[.5, .7], [.1, .1]]]
304+
lc1 = mc.LineCollection(lines1, linestyles="--", lw=3)
305+
lc2 = mc.LineCollection(lines2, linestyles="-.")
306+
lc3 = mc.LineCollection(lines3, linestyles=":", lw=.5)
307+
308+
fig, ax = plt.subplots()
309+
ax.add_collection(lc1)
310+
ax.add_collection(lc2)
311+
ax.add_collection(lc3)
312+
313+
leg = ax.legend([lc1, lc2, lc3], ["line1", "line2", 'line 3'])
314+
h1, h2, h3 = leg.legendHandles
315+
316+
for oh, lh in zip((lc1, lc2, lc3), (h1, h2, h3)):
317+
assert oh.get_linestyles()[0][1] == lh._dashSeq
318+
assert oh.get_linestyles()[0][0] == lh._dashOffset
319+
320+
299321
if __name__ == '__main__':
300322
import nose
301323
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)
302-

0 commit comments

Comments
 (0)