-
Notifications
You must be signed in to change notification settings - Fork 49
/
utils.py
122 lines (83 loc) · 2.77 KB
/
utils.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib as mpl
mpl.rcParams['lines.linewidth'] = 2.0
def three_frame(world, n_seq, seed=17):
"""Draw three timesteps.
world: object with step, loop, and draw
n_seq: 3-tuple, number of steps before each draw
seed: random see for NumPy
"""
np.random.seed(seed)
plt.figure(figsize=(10, 4))
for i, n in enumerate(n_seq):
plt.subplot(1, 3, i+1)
world.loop(n)
world.draw()
plt.tight_layout()
def savefig(filename, **options):
"""Save the current figure.
Keyword arguments are passed along to plt.savefig
https://matplotlib.org/api/_as_gen/matplotlib.pyplot.savefig.html
filename: string
"""
print("Saving figure to file", filename)
plt.savefig(filename, **options)
def underride(d, **options):
"""Add key-value pairs to d only if key is not in d.
d: dictionary
options: keyword args to add to d
"""
for key, val in options.items():
d.setdefault(key, val)
return d
def decorate(**options):
"""Decorate the current axes.
Call decorate with keyword arguments like
decorate(title='Title',
xlabel='x',
ylabel='y')
The keyword arguments can be any of the axis properties
https://matplotlib.org/api/axes_api.html
In addition, you can use `legend=False` to suppress the legend.
And you can use `loc` to indicate the location of the legend
(the default value is 'best')
"""
loc = options.pop('loc', 'best')
if options.pop('legend', True):
legend(loc=loc)
plt.gca().set(**options)
plt.tight_layout()
def legend(**options):
"""Draws a legend only if there is at least one labeled item.
options are passed to plt.legend()
https://matplotlib.org/api/_as_gen/matplotlib.pyplot.legend.html
"""
underride(options, loc='best', frameon=False)
ax = plt.gca()
handles, labels = ax.get_legend_handles_labels()
if handles:
ax.legend(handles, labels, **options)
def set_palette(*args, **kwds):
"""Set the matplotlib color cycler.
args, kwds: same as for sns.color_palette
Also takes a boolean kwd, `reverse`, to indicate
whether the order of the palette should be reversed.
returns: list of colors
"""
reverse = kwds.pop('reverse', False)
palette = sns.color_palette(*args, **kwds)
palette = list(palette)
if reverse:
palette.reverse()
cycler = plt.cycler(color=palette)
plt.gca().set_prop_cycle(cycler)
return palette
def anchor_legend(x, y):
"""Put the legend at the given locationself.
x: axis coordinate
y: axis coordinate
"""
plt.legend(bbox_to_anchor=(x, y), loc='upper left', ncol=1)