def configure_legend(ax, autofmt_xdate=True, change_colors=False,<EOL>rotation=<NUM_LIT:30>, ha='<STR_LIT:right>'):
chartBox = ax.get_position()<EOL>ax.set_position([chartBox.x0, chartBox.y0,<EOL>chartBox.width * <NUM_LIT>, chartBox.height])<EOL>handles, labels = ax.get_legend_handles_labels()<EOL>handles_and_labels_sorted = sorted(zip(handles, labels),<EOL>key=lambda x: x[<NUM_LIT:0>].get_ydata()[-<NUM_LIT:1>],<EOL>reverse=True)<EOL>handles_sorted = [h[<NUM_LIT:0>] for h in handles_and_labels_sorted]<EOL>labels_sorted = [h[<NUM_LIT:1>] for h in handles_and_labels_sorted]<EOL>if change_colors:<EOL><INDENT>for handle, color in zip(handles_sorted,<EOL>cycle(COLORS)):<EOL><INDENT>handle.set_color(color)<EOL><DEDENT><DEDENT>ax.legend(handles=handles_sorted,<EOL>labels=labels_sorted,<EOL>frameon=True,<EOL>framealpha=<NUM_LIT:0.5>,<EOL>loc='<STR_LIT>',<EOL>bbox_to_anchor=(<NUM_LIT>, <NUM_LIT:1>),<EOL>fontsize='<STR_LIT>')<EOL>if autofmt_xdate:<EOL><INDENT>for label in ax.get_xticklabels():<EOL><INDENT>label.set_ha(ha)<EOL>label.set_rotation(rotation)<EOL><DEDENT><DEDENT>
Format legend for perf attribution plots:
- put legend to the right of plot instead of overlapping with it
- make legend order match up with graph lines
- set colors according to colormap