-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Legend tuple handler improve #2904
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
Closed
tacaswell
wants to merge
6
commits into
matplotlib:master
from
tacaswell:legend-tuple-handler-improve
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9e65dcd
fix legend_handler.HandlerNpoints.get_xdata of wrong xdescent interpr…
leejjoon 0768294
modify legend_hadler.HandlerTuple to display multiple handles side-by…
leejjoon b2e5974
MNT : move import to top level
tacaswell 2e31317
DOC : added docstring
tacaswell 9d87c9e
MNT : python3 compatibility updates
tacaswell 938ffd2
STY : pep8 on legend example
tacaswell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import matplotlib.pyplot as plt | ||
| from matplotlib.legend_handler import HandlerTuple | ||
|
|
||
| fig, (ax1, ax2) = plt.subplots(2, 1) | ||
| p1 = ax1.scatter([1], [5], c='r', marker='s', s=100) | ||
| p2 = ax1.scatter([3], [2], c='b', marker='o', s=100) | ||
|
|
||
| l = ax1.legend([(p1, p2)], ['points'], scatterpoints=1, | ||
| handler_map={tuple: HandlerTuple(ndivide=0)}) | ||
|
|
||
| ind = [1, 2, 3] | ||
| pos1 = [1, 3, 2] | ||
| neg1 = [2, 1, 4] | ||
| width = [0.5, 0.5, 0.5] | ||
|
|
||
| rpos1 = ax2.bar(ind, pos1, width=0.5, color='k', label='+1') | ||
| rneg1 = ax2.bar(ind, neg1, width=0.5, color='w', hatch='///', label='-1') | ||
|
|
||
| l = ax2.legend([(rpos1, rneg1)], ['Test'], | ||
| handler_map={(rpos1, rneg1): HandlerTuple(ndivide=0, pad=0.)}) | ||
|
|
||
| plt.show() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,6 +29,7 @@ def legend_artist(self, legend, orig_handle, fontsize, handlebox): | |
|
|
||
| import six | ||
| from six.moves import zip | ||
| from itertools import cycle | ||
|
|
||
| import numpy as np | ||
|
|
||
|
|
@@ -150,13 +151,14 @@ def get_xdata(self, legend, xdescent, ydescent, width, height, fontsize): | |
| if numpoints > 1: | ||
| # we put some pad here to compensate the size of the | ||
| # marker | ||
| xdata = np.linspace(-xdescent + self._marker_pad * fontsize, | ||
| width - self._marker_pad * fontsize, | ||
| pad = self._marker_pad * fontsize | ||
| xdata = np.linspace(-xdescent + pad, | ||
| -xdescent + width - pad, | ||
| numpoints) | ||
| xdata_marker = xdata | ||
| elif numpoints == 1: | ||
| xdata = np.linspace(-xdescent, width, 2) | ||
| xdata_marker = [0.5 * width - 0.5 * xdescent] | ||
| xdata = np.linspace(-xdescent, -xdescent+width, 2) | ||
| xdata_marker = [-xdescent + 0.5 * width] | ||
|
|
||
| return xdata, xdata_marker | ||
|
|
||
|
|
@@ -496,6 +498,7 @@ def create_artists(self, legend, orig_handle, | |
|
|
||
| return artists | ||
|
|
||
|
|
||
| class HandlerStem(HandlerNpointsYoffsets): | ||
| """ | ||
| Handler for Errorbars | ||
|
|
@@ -562,21 +565,60 @@ def create_artists(self, legend, orig_handle, | |
|
|
||
| class HandlerTuple(HandlerBase): | ||
| """ | ||
| Handler for Tuple | ||
| Handler for Tuple. | ||
|
|
||
| Additional kwargs are passed through to `HandlerBase`. | ||
|
|
||
| Parameters | ||
| ---------- | ||
|
|
||
| ndivide : int, optional | ||
| The number of sections to divide the legend area into. If 0, | ||
| use the length of the input tuple. | ||
|
|
||
|
|
||
| pad : float, optional | ||
| If None, fall back to `legend.borderpad` as the default. | ||
| In units of fraction of font size. | ||
|
|
||
|
|
||
|
|
||
| """ | ||
| def __init__(self, **kwargs): | ||
| def __init__(self, ndivide=1, pad=None, **kwargs): | ||
|
|
||
| self._ndivide = ndivide | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs a docstring to describe these new arguments. |
||
| self._pad = pad | ||
| HandlerBase.__init__(self, **kwargs) | ||
|
|
||
| def create_artists(self, legend, orig_handle, | ||
| xdescent, ydescent, width, height, fontsize, | ||
| trans): | ||
|
|
||
| handler_map = legend.get_legend_handler_map() | ||
|
|
||
| if self._ndivide == 0: | ||
| ndivide = len(orig_handle) | ||
| else: | ||
| ndivide = self._ndivide | ||
|
|
||
| if self._pad is None: | ||
| pad = legend.borderpad * fontsize | ||
| else: | ||
| pad = self._pad * fontsize | ||
|
|
||
| if ndivide > 1: | ||
| width = (width - pad*(ndivide - 1)) / ndivide | ||
|
|
||
| xds = [xdescent - (width + pad) * i for i in range(ndivide)] | ||
| xds_cycle = cycle(xds) | ||
|
|
||
| a_list = [] | ||
| for handle1 in orig_handle: | ||
| handler = legend.get_legend_handler(handler_map, handle1) | ||
| _a_list = handler.create_artists(legend, handle1, | ||
| xdescent, ydescent, width, height, | ||
| six.next(xds_cycle), | ||
| ydescent, | ||
| width, height, | ||
| fontsize, | ||
| trans) | ||
| a_list.extend(_a_list) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dictionary comprehensions weren't added until py2.7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, why didn't Travis pick that up? I thought it went through all examples...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It goes through the examples as part of the doc build, which is probably
not py2.6.
On Mon, Apr 20, 2015 at 11:34 AM, OceanWolf notifications@github.com
wrote: