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

Skip to content

Fix collection legend handlers #7832

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 15, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
FIX: pull the unscaled dashes from the line collection
To prevent double-scaling of the pattern in the legend

Closes #7814
  • Loading branch information
tacaswell committed Jan 15, 2017
commit 9893d4fa2e849f2574996bbd72738d9fb9a0b65f
5 changes: 2 additions & 3 deletions lib/matplotlib/legend_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,11 @@ def get_numpoints(self, legend):

def _default_update_prop(self, legend_handle, orig_handle):
lw = orig_handle.get_linewidths()[0]
dashes = orig_handle.get_dashes()[0]
dashes = orig_handle._us_linestyles[0]
color = orig_handle.get_colors()[0]
legend_handle.set_color(color)
legend_handle.set_linestyle(dashes)
legend_handle.set_linewidth(lw)
if dashes[0] is not None: # dashed line
legend_handle.set_dashes(dashes[1])

def create_artists(self, legend, orig_handle,
xdescent, ydescent, width, height, fontsize, trans):
Expand Down
25 changes: 23 additions & 2 deletions lib/matplotlib/tests/test_legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import matplotlib as mpl
import matplotlib.patches as mpatches
import matplotlib.transforms as mtrans

import matplotlib.collections as mc
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not very consistent, but this is generally imported as mcoll or mcollections. Also, second blank line should be re-added.


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


@cleanup
def test_linecollection_scaled_dashes():
lines1 = [[(0, .5), (.5, 1)], [(.3, .6), (.2, .2)]]
lines2 = [[[0.7, .2], [.8, .4]], [[.5, .7], [.6, .1]]]
lines3 = [[[0.6, .2], [.8, .4]], [[.5, .7], [.1, .1]]]
lc1 = mc.LineCollection(lines1, linestyles="--", lw=3)
lc2 = mc.LineCollection(lines2, linestyles="-.")
lc3 = mc.LineCollection(lines3, linestyles=":", lw=.5)

fig, ax = plt.subplots()
ax.add_collection(lc1)
ax.add_collection(lc2)
ax.add_collection(lc3)

leg = ax.legend([lc1, lc2, lc3], ["line1", "line2", 'line 3'])
h1, h2, h3 = leg.legendHandles

for oh, lh in zip((lc1, lc2, lc3), (h1, h2, h3)):
assert oh.get_linestyles()[0][1] == lh._dashSeq
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is going in 2.0, it probably should be assert_equal.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert will work fine, though it is not as elegant.

assert oh.get_linestyles()[0][0] == lh._dashOffset


if __name__ == '__main__':
import nose
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)