Skip to content

Instantly share code, notes, and snippets.

@jeasinema
Last active September 2, 2019 06:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeasinema/8ec482ef89282fdb6c83e2fe0c912ced to your computer and use it in GitHub Desktop.
Save jeasinema/8ec482ef89282fdb6c83e2fe0c912ced to your computer and use it in GitHub Desktop.
Create customized legend
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
import seaborn as sns
import numpy as np
fig = plt.figure()
fig.set_size_inches(18, 3)
ax = fig.subplots(1)
label = ['Expert', 'Demo', 'Pre-train', 'POfD', 'Penalty', 'Penalty + Ann.', 'Ours']
flatui = ["#9b59b6", "#999900", "#3498db", "#95a5a6", "#009900", "#fc9272", "#e74c3c"]
clrs = sns.color_palette(flatui)
custom_lines = [Line2D([0], [0], color=clrs[0], lw=2, linestyle='--'),
Line2D([0], [0], color=clrs[1], lw=2, linestyle='--'),
Line2D([0], [0], color=clrs[2], lw=2, linestyle='-'),
Line2D([0], [0], color=clrs[3], lw=2, linestyle='-'),
Line2D([0], [0], color=clrs[4], lw=2, linestyle='-'),
Line2D([0], [0], color=clrs[5], lw=2, linestyle='-'),
Line2D([0], [0], color=clrs[6], lw=2, linestyle='-')]
with sns.axes_style('darkgrid'):
for i in range(len(label)):
ax.plot(np.arange(10), np.ones(10), c=clrs[i])
box = ax.get_position()
ax.set_position([box.x0, box.y0 + box.height * 0.1,
box.width, box.height * 0.9])
ax.legend(handles=custom_lines, labels=label, ncol=7, shadow=False, facecolor='white', handlelength=2.0, columnspacing=1.0, handletextpad=0.4, loc='upper center', bbox_to_anchor=(0.5, -0.1))
fig.savefig('legend.pdf')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment