Skip to content
Open
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
1 change: 1 addition & 0 deletions boilerplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ def boilerplate_gen():
'cla',
'grid',
'legend',
'uniformlegend',
'table',
'text',
'annotate',
Expand Down
394 changes: 300 additions & 94 deletions lib/matplotlib/axes/_axes.py

Large diffs are not rendered by default.

97 changes: 97 additions & 0 deletions lib/matplotlib/legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -993,3 +993,100 @@ def draggable(self, state=None, use_blit=False, update="loc"):
self._draggable = None

return self._draggable


class UniformLegend(Legend):
def __str__(self):
return "uniformLegend"

def __init__(self, parent, handles, labels,uniform_size,
loc=None,
numpoints=None, # the number of points in the legend line
markerfirst=True, # controls ordering (left-to-right) of
# legend marker and label
scatterpoints=None, # number of scatter points
scatteryoffsets=None,
prop=None, # properties for the legend texts
fontsize=None, # keyword to set font size directly

# spacing & pad defined as a fraction of the font-size
borderpad=None, # the whitespace inside the legend border
labelspacing=None, # the vertical space between the legend
# entries
handlelength=None, # the length of the legend handles
handleheight=None, # the height of the legend handles
handletextpad=None, # the pad between the legend handle
# and text
borderaxespad=None, # the pad between the axes and legend
# border
columnspacing=None, # spacing between columns

ncol=1, # number of columns
mode=None, # mode for horizontal distribution of columns.
# None, "expand"

fancybox=None, # True use a fancy box, false use a rounded
# box, none use rc
shadow=None,
title=None, # set a title for the legend

framealpha=None, # set frame alpha

bbox_to_anchor=None, # bbox that the legend will be anchored.
bbox_transform=None, # transform for the bbox
frameon=None
):
uniformHandlerMap = {
StemContainer:
legend_handler.HandlerUniformStem(uniform_size=uniform_size),
ErrorbarContainer:
legend_handler.HandlerUniformErrorBar(uniform_size=uniform_size),
Line2D: legend_handler.HandlerUniformLine2D(uniform_size=uniform_size),
PathCollection:
legend_handler.HandlerPathCollection(sizes=[uniform_size]*3),
RegularPolyCollection:
legend_handler.HandlerRegularPolyCollection(sizes=[uniform_size]*3),
CircleCollection:
legend_handler.HandlerCircleCollection(sizes=[uniform_size]*3),
}

Legend.__init__(self,parent,handles,labels,
loc=loc,
numpoints=numpoints, # the number of points in the legend line
markerscale=None, # the relative size of legend markers
# vs. original
markerfirst=markerfirst, # controls ordering (left-to-right) of
# legend marker and label
scatterpoints=scatterpoints, # number of scatter points
scatteryoffsets=scatteryoffsets,
prop=prop, # properties for the legend texts
fontsize=fontsize, # keyword to set font size directly

# spacing & pad defined as a fraction of the font-size
borderpad=borderpad, # the whitespace inside the legend border
labelspacing=labelspacing, # the vertical space between the legend
# entries
handlelength=handlelength, # the length of the legend handles
handleheight=handleheight, # the height of the legend handles
handletextpad=handletextpad, # the pad between the legend handle
# and text
borderaxespad=borderaxespad, # the pad between the axes and legend
# border
columnspacing=columnspacing, # spacing between columns

ncol=1, # number of columns
mode=mode, # mode for horizontal distribution of columns.
# None, "expand"

fancybox=fancybox, # True use a fancy box, false use a rounded
# box, none use rc
shadow=shadow,
title=title, # set a title for the legend

framealpha=framealpha, # set frame alpha

bbox_to_anchor=bbox_to_anchor, # bbox that the legend will be anchored.
bbox_transform=bbox_transform, # transform for the bbox
frameon=frameon, # draw frame
handler_map=uniformHandlerMap,
)
49 changes: 49 additions & 0 deletions lib/matplotlib/legend_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,3 +617,52 @@ def create_artists(self, legend, orig_handle,
self.update_prop(p, orig_handle, legend)
p.set_transform(trans)
return [p]
class HandlerUniformLine2D(HandlerLine2D):
"""
Handler for Uniform sized Line2D instances
"""

def __init__(self, uniform_size, **kw):
self._uniform_size = uniform_size
HandlerLine2D.__init__(self, **kw)

def create_artists(self, legend, orig_handle,
xdescent, ydescent, width, height, fontsize,
trans):
artists = HandlerLine2D.create_artists(self, legend, orig_handle,
xdescent, ydescent, width, height, fontsize,
trans)
artists[-1].set_markersize(self._uniform_size)
return artists
class HandlerUniformErrorBar(HandlerErrorbar):
"""
Handler for Uniform sized Error instances
"""

def __init__(self, uniform_size, **kw):
self._uniform_size = uniform_size
HandlerErrorbar.__init__(self, **kw)

def create_artists(self, legend, orig_handle,
xdescent, ydescent, width, height, fontsize,
trans):
artists = HandlerErrorbar.create_artists(self, legend, orig_handle,
xdescent, ydescent, width, height, fontsize,
trans)
artists[-1].set_markersize(self._uniform_size)
return artists
class HandlerUniformStem(HandlerStem):
def __init__(self, uniform_size, **kw):

HandlerStem.__init__(self, **kw)
self._uniform_size = uniform_size

def create_artists(self, legend, orig_handle,
xdescent, ydescent, width, height, fontsize,
trans):
artists = HandlerStem.create_artists(self, legend, orig_handle,
xdescent, ydescent,
width, height, fontsize,
trans)
artists[0].set_markersize(self._uniform_size)
return artists
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions lib/matplotlib/tests/test_legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
import matplotlib as mpl
import matplotlib.patches as mpatches

import matplotlib.legend_handler as mlegend_handler
from matplotlib.collections import PathCollection
import warnings


@image_comparison(baseline_images=['legend_auto1'], remove_text=True)
def test_legend_auto1():
Expand Down Expand Up @@ -240,6 +244,38 @@ def test_legend_stackplot():
ax.legend(loc=0)


@image_comparison(baseline_images=['scatter_uni_size_off'], extensions=['png'])
def test_scatterplot_uni_size_warning():
"""
Expect warning to be thrown when specifying 'scatter_uni_size' and a custom
handler. Check that custom handler takes precedence.
"""
x1, y1 = [1, 1]
x2, y2 = [1, 2]
plt.figure()
plt.scatter(x1, y1, marker='o', label='first', s=20, c='b')
plt.scatter(x2, y2, marker='o', label='second', s=50, c='r')

with warnings.catch_warnings(record=True) as w:
plt.legend(handler_map={
PathCollection: mlegend_handler.HandlerPathCollection()},
scatter_uni_size=30)
assert_equal(len(w), 1)


@image_comparison(baseline_images=['scatter_uni_size_on'], extensions=['png'])
def test_scatterplot_uni_size_success_image():
"""
Test that 'scatter_uni_size' sets legend handles for scatter plots to be
of the specified size.
"""
x1, y1 = [1, 1]
x2, y2 = [1, 2]
fig = plt.figure()
plt.scatter(x1, y1, marker='o', label='first', s=20, c='b')
plt.scatter(x2, y2, marker='o', label='second', s=50, c='r')
plt.legend(scatter_uni_size=70)

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