Skip to content

Instantly share code, notes, and snippets.

@evanmiltenburg
Created February 13, 2018 15:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save evanmiltenburg/3758d7d0a41abfcbb20f6ba49fba9da8 to your computer and use it in GitHub Desktop.
Save evanmiltenburg/3758d7d0a41abfcbb20f6ba49fba9da8 to your computer and use it in GitHub Desktop.
Circles in legend
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib.lines import Line2D
my_palette = sns.color_palette("cubehelix", 3)
sns.set_palette(my_palette)
def legend_circles(labels, palette, loc=1, markersize=10, marker='o', padding=0):
"Make a legend where the color is indicated by a circle."
legend_markers = [Line2D(range(1), range(1),
linewidth=0, # Invisible line
marker=marker,
markersize=markersize,
markerfacecolor=palette[i]) for i in range(len(labels))]
return plt.legend(legend_markers, labels, numpoints=1, loc=loc, handletextpad=padding)
plt.plot([1,2,3])
plt.plot([0,1,3])
plt.plot([3,2,1])
legend = legend_circles(['a','b','c'], my_palette,loc=0)
plt.show()
@evanmiltenburg
Copy link
Author

Example output:
figure_1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment