Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Line2D handler which lets you tweak the x location of the markers.
from matplotlib.legend_handler import HandlerLine2D
class HandlerXoffset(HandlerLine2D):
def __init__(self, marker_pad=0.3, numpoints=1, x_offset=0, **kw):
HandlerLine2D.__init__(self, marker_pad=marker_pad, numpoints=numpoints, **kw)
self._xoffset = x_offset
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
xdata = np.linspace(-xdescent + self._marker_pad * fontsize,
width - self._marker_pad * fontsize,
numpoints) - self._xoffset
xdata_marker = xdata
elif numpoints == 1:
xdata = np.linspace(-xdescent, width, 2) - self._xoffset
xdata_marker = [0.5 * width - 0.5 * xdescent - self._xoffset]
print xdata, self._xoffset
print xdata_marker
return xdata, xdata_marker
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment