X Tutup
Skip to content
Merged
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
10 changes: 4 additions & 6 deletions lib/matplotlib/legend_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,22 +147,20 @@ def get_numpoints(self, legend):

def get_xdata(self, legend, xdescent, ydescent, width, height, fontsize):
numpoints = self.get_numpoints(legend)

if numpoints > 1:
# we put some pad here to compensate the size of the
# marker
# we put some pad here to compensate the size of the marker
pad = self._marker_pad * fontsize
xdata = np.linspace(-xdescent + pad,
-xdescent + width - pad,
numpoints)
xdata_marker = xdata
elif numpoints == 1:
xdata = np.linspace(-xdescent, -xdescent+width, 2)
else:
xdata = np.linspace(-xdescent, -xdescent + width, 2)
xdata_marker = [-xdescent + 0.5 * width]

return xdata, xdata_marker



class HandlerNpointsYoffsets(HandlerNpoints):
def __init__(self, numpoints=None, yoffsets=None, **kw):
HandlerNpoints.__init__(self, numpoints=numpoints, **kw)
Expand Down
8 changes: 8 additions & 0 deletions lib/matplotlib/tests/test_legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,11 @@ def test_linecollection_scaled_dashes():
for oh, lh in zip((lc1, lc2, lc3), (h1, h2, h3)):
assert oh.get_linestyles()[0][1] == lh._dashSeq
assert oh.get_linestyles()[0][0] == lh._dashOffset


def test_handler_numpoints():
'''test legend handler with numponts less than or equal to 1'''
# related to #6921 and PR #8478
fig, ax = plt.subplots()
ax.plot(range(5), label='test')
ax.legend(numpoints=0.5)
X Tutup