-
Notifications
You must be signed in to change notification settings - Fork 4
/
custom_legends.py
62 lines (52 loc) · 2.24 KB
/
custom_legends.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import matplotlib.pyplot as plt
import numpy as np
__all__ = ['colorLegend',
'symbolLegend']
def colorLegend(colors, labels, alphas=None, edgecolor='black',loc='best', axh=None, **legendKwargs):
"""Custom matplotlib legend with colors and labels etc.
Useful in cases where it is awkward to include labels on the appropriate plot() objects.
Parameters specify the characteristics of each line in the legend.
Parameters
----------
colors : list of valid matplotlib colors
labels : list of strings
alphas : list of alpha values
edgecolor : single valid matplotlib color
All remaining kwargs are passed to legend()
"""
if axh is None:
axh = plt.gca()
if alphas is None:
alphas = np.ones(len(colors))
circles = (plt.Circle((0, 0), fc=c, ec=edgecolor, alpha=a) for c, a in zip(colors, alphas))
lh = axh.legend(circles,
labels,
loc=loc,
**legendKwargs)
return lh
def symbolLegend(symbols, labels, facecolors=None, edgecolors=None, alphas=None,loc='best', **legendKwargs):
"""Custom matplotlib legend with lines, symbols and labels etc.
Useful in cases where it is awkward to include labels on the appropriate plot() objects.
Parameters specify the characteristics of each line in the legend.
Parameters
----------
symbols : list of valid matplotlib symbols
E.g. 'xs^*.<>' or other matplotlib.markers
labels : list of strings
facecolors : list of valid matplotlib colors
edgecolors : list of valid matplotlib colors
alphas : list of alpha values
All remaining kwargs are passed to legend()
"""
if alphas is None:
alphas = np.ones(len(symbols))
if edgecolors is None:
edgecolors = ['black'] * len(symbols)
if facecolors is None:
facecolors = ['white'] * len(symbols)
lh = plt.legend((plt.Line2D([0], [0], ls = '', marker = s, markerfacecolor = mfc, markeredgecolor = ec, alpha = a) for s, mfc, ec, a in zip(symbols, facecolors, edgecolors, alphas)),
labels,
loc,
numpoints=1,
**legendKwargs)
return lh