X Tutup
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 22 additions & 0 deletions examples/pylab_examples/legend_demo5.py
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.)})
Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
Member

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...

Copy link
Copy Markdown
Member

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:

In examples/pylab_examples/legend_demo5.py
#2904 (comment):

+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.)})
    

Hmm, why didn't Travis pick that up? I thought it went through all
examples...


Reply to this email directly or view it on GitHub
https://github.com/matplotlib/matplotlib/pull/2904/files#r28699293.


plt.show()
56 changes: 49 additions & 7 deletions lib/matplotlib/legend_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -496,6 +498,7 @@ def create_artists(self, legend, orig_handle,

return artists


class HandlerStem(HandlerNpointsYoffsets):
"""
Handler for Errorbars
Expand Down Expand Up @@ -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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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)
Expand Down
X Tutup