solve the problem with an exception
I suspect you need:
p1, = plt.plot(time_model, rv_model_primary, 'k-')
p2, = plt.plot(time_model_sec, rv_model_secondary, 'k--')
p3, = plt.plot(time_obs, rv_obs_primary, 'bo')
p4, = plt.plot(time_obs_apg, rv_obs_primary_apg, 'ro')
p5, = plt.plot(time_obs_apg_sec, rv_obs_secondary_apg, 'rs')
plot Line2D ( , ), , . , .
:
plt.legend([p1,p2,p3,(p5,p4)],["Primary", "Secondary", "XYZ", "This Work"],
handler_map={p4:HandlerLine2D(numpoints=2), p5:HandlerLine2D(numpoints=1)})
, .
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:
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
time_model = time_model_sec = time_obs = time_obs_apg = time_obs_apg_sec = range(5)
rv_model_primary = np.random.rand(5)
rv_model_secondary = np.random.rand(5)
rv_obs_primary = np.random.rand(5)
rv_obs_primary_apg = np.random.rand(5)
rv_obs_secondary_apg = np.random.rand(5)
p1,=plt.plot(time_model, rv_model_primary, 'k-')
p2,=plt.plot(time_model_sec, rv_model_secondary, 'k--')
p3,=plt.plot(time_obs, rv_obs_primary, 'bo')
p4,=plt.plot(time_obs_apg, rv_obs_primary_apg, 'ro')
p5,=plt.plot(time_obs_apg_sec, rv_obs_secondary_apg, 'rs')
plt.legend([p1,p2,p3,(p5,p4)],
["Primary", "Secondary", "XYZ", "This Work"],
handler_map={p4:HandlerXoffset(x_offset=10),
p5:HandlerXoffset(x_offset=-10)})
gist
, , x_offset, , , , , , , .
