from matplotlib.patches import Rectangle
from matplotlib.legend_handler import HandlerPatch, update_from_first_child
def patch_func_left_half(legend, orig_handle,
xdescent, ydescent,
width, height, fontsize):
half_width = width/2.
p = Rectangle(xy=(-xdescent, -ydescent),
width=half_width, height=height)
return p
def patch_func_right_half(legend, orig_handle,
xdescent, ydescent,
width, height, fontsize):
half_width = width/2.
p = Rectangle(xy=(-xdescent+half_width, -ydescent),
width=half_width, height=height)
return p
bar_handler_left_half = HandlerPatch(patch_func_left_half,
update_func=update_from_first_child)
bar_handler_right_half = HandlerPatch(patch_func_right_half,
update_func=update_from_first_child)
import matplotlib.pyplot as plt
ax = plt.subplot(111)
ind = [1,2,3]
pos1 = [1, 3, 2]
neg1 = [2, 1, 4]
width=[0.5, 0.5, 0.5]
rpos1 = ax.bar(ind, pos1, width, color='k', label='+1')
rneg1 = ax.bar(ind, neg1, width, color='w', hatch='///', label='-1')
ax.legend(((rpos1, rneg1),), ('one', 'two'),
handler_map={rpos1:bar_handler_left_half,
rneg1:bar_handler_right_half})
plt.show()