Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f00274f

Browse files
authored
Merge pull request #7832 from tacaswell/fix_collection_legend_handlers
Fix collection legend handlers
2 parents 112d48e + 9a75feb commit f00274f

File tree

6 files changed

+39
-16
lines changed

6 files changed

+39
-16
lines changed

lib/matplotlib/collections.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,7 @@ def update_from(self, other):
788788
self._facecolors = other._facecolors
789789
self._linewidths = other._linewidths
790790
self._linestyles = other._linestyles
791+
self._us_linestyles = other._us_linestyles
791792
self._pickradius = other._pickradius
792793
self._hatch = other._hatch
793794

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/lines.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,7 +1319,9 @@ def update_from(self, other):
13191319
self._markeredgecolor = other._markeredgecolor
13201320
self._markeredgewidth = other._markeredgewidth
13211321
self._dashSeq = other._dashSeq
1322+
self._us_dashSeq = other._us_dashSeq
13221323
self._dashOffset = other._dashOffset
1324+
self._us_dashOffset = other._us_dashOffset
13231325
self._dashcapstyle = other._dashcapstyle
13241326
self._dashjoinstyle = other._dashjoinstyle
13251327
self._solidcapstyle = other._solidcapstyle

lib/matplotlib/patches.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,9 @@ def update_from(self, other):
199199
self._facecolor = other._facecolor
200200
self._fill = other._fill
201201
self._hatch = other._hatch
202-
self._linewidth = other._linewidth
203-
# Use setters, getters where we need the extra work they do.
204-
self.set_linestyle(other._linestyle) # also sets dash properties
202+
# copy the unscaled dash pattern
203+
self._us_dashes = other._us_dashes
204+
self.set_linewidth(other._linewidth) # also sets dash properties
205205
self.set_transform(other.get_data_transform())
206206

207207
def get_extents(self):

lib/matplotlib/tests/test_coding_standards.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,6 @@ def test_pep8_conformance_installed_files():
207207
'tests/test_delaunay.py',
208208
'tests/test_dviread.py',
209209
'tests/test_image.py',
210-
'tests/test_legend.py',
211210
'tests/test_lines.py',
212211
'tests/test_mathtext.py',
213212
'tests/test_rcparams.py',

lib/matplotlib/tests/test_legend.py

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@
1212
import numpy as np
1313

1414
from matplotlib.testing.decorators import image_comparison, cleanup
15-
from matplotlib.cbook import MatplotlibDeprecationWarning
1615
import matplotlib.pyplot as plt
1716
import matplotlib as mpl
18-
import matplotlib.patches as mpatches
1917
import matplotlib.transforms as mtrans
18+
import matplotlib.collections as mcollections
2019

2120

2221
@image_comparison(baseline_images=['legend_auto1'], remove_text=True)
@@ -110,7 +109,8 @@ def test_fancy():
110109
plt.subplot(121)
111110
plt.scatter(list(xrange(10)), list(xrange(10, 0, -1)), label='XX\nXX')
112111
plt.plot([5] * 10, 'o--', label='XX')
113-
plt.errorbar(list(xrange(10)), list(xrange(10)), xerr=0.5, yerr=0.5, label='XX')
112+
plt.errorbar(list(xrange(10)), list(xrange(10)), xerr=0.5,
113+
yerr=0.5, label='XX')
114114
plt.legend(loc="center left", bbox_to_anchor=[1.0, 0.5],
115115
ncol=2, shadow=True, title="My legend", numpoints=1)
116116

@@ -123,7 +123,8 @@ def test_framealpha():
123123
plt.legend(framealpha=0.5)
124124

125125

126-
@image_comparison(baseline_images=['scatter_rc3', 'scatter_rc1'], remove_text=True)
126+
@image_comparison(baseline_images=['scatter_rc3', 'scatter_rc1'],
127+
remove_text=True)
127128
def test_rc():
128129
# using subplot triggers some offsetbox functionality untested elsewhere
129130
fig = plt.figure()
@@ -221,8 +222,8 @@ def test_warn_args_kwargs(self):
221222
ax.legend((lnc, lns), labels=('a', 'b'))
222223

223224
warn.assert_called_with("You have mixed positional and keyword "
224-
"arguments, some input will be "
225-
"discarded.")
225+
"arguments, some input will be "
226+
"discarded.")
226227

227228

228229
@image_comparison(baseline_images=['legend_stackplot'], extensions=['png'])
@@ -273,10 +274,10 @@ def test_nanscatter():
273274

274275
@image_comparison(baseline_images=['not_covering_scatter'], extensions=['png'])
275276
def test_not_covering_scatter():
276-
colors = ['b','g','r']
277+
colors = ['b', 'g', 'r']
277278

278279
for n in range(3):
279-
plt.scatter([n,], [n,], color=colors[n])
280+
plt.scatter([n], [n], color=colors[n])
280281

281282
plt.legend(['foo', 'foo', 'foo'], loc='best')
282283
plt.gca().set_xlim(-0.5, 2.2)
@@ -296,7 +297,28 @@ def test_not_covering_scatter_transform():
296297
plt.legend(['foo', 'bar'], loc='best')
297298

298299

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

0 commit comments

Comments
 (0)