-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplot.py
More file actions
169 lines (127 loc) · 3.62 KB
/
plot.py
File metadata and controls
169 lines (127 loc) · 3.62 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import numpy as np
import matplotlib.pylab as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import colors
import pandas as pd
import seaborn as sns
#Util class to plot different types of plots
#Use tex for labelling the plots
plt.rcParams['text.usetex'] = True
fontsize = 12
fontscale = 1.5
fontsize_ticks = 10
sns.set(font_scale = fontscale)
#Saves a plot figure in both .png and .svg format
def save_figure(filename):
plt.savefig(filename + '.png')
plt.savefig(filename + '.svg')
#Plots an array with 1 std dev.
def plot_CI(values:np.array,
xlabel,
ylabel,
filename):
fig, ax = plt.subplots()
sns.set(font_scale = fontscale)
ax.set_xlabel(xlabel, fontsize = fontsize)
ax.set_ylabel(ylabel, fontsize = fontsize)
num_rows = values.shape[0]
num_columns = values.shape[1]
episodes = np.zeros(num_columns)
mean = np.zeros(num_columns)
stdev = np.zeros(num_columns)
for c in range(0, num_columns):
mean[c] = np.mean(values[:,c])
stdev[c] = np.std(values[:,c])
episodes[c] = c
lower_curve = mean - stdev
upper_curve = mean + stdev
plt.plot(mean)
plt.fill_between(episodes, lower_curve, upper_curve, color='b')
plt.tight_layout()
save_figure(filename)
plt.clf()
#Plots two arrays with 1 std dev.
def compare_plot_CI(values1: np.array,
label1,
values2:np.array,
label2,
xlabel,
ylabel,
filename,
pfailgraph = False):
fig, ax = plt.subplots()
sns.set(font_scale = fontscale)
ax.set_xlabel(xlabel, fontsize=fontsize)
ax.set_ylabel(ylabel, fontsize =fontsize)
num_rows = values1.shape[0]
num_columns = values1.shape[1]
episodes = np.zeros(num_columns)
mean = np.zeros(num_columns)
stdev = np.zeros(num_columns)
for c in range(0, num_columns):
mean[c] = np.mean(values1[:,c])
stdev[c] = np.std(values1[:,c])
episodes[c] = c
lower_curve = mean - stdev
upper_curve = mean + stdev
plt.plot(mean, label = label1)
plt.fill_between(episodes, lower_curve, upper_curve, color='b', alpha = 0.1)
num_rows = values2.shape[0]
num_columns = values2.shape[1]
episodes = np.zeros(num_columns)
mean = np.zeros(num_columns)
stdev = np.zeros(num_columns)
for c in range(0, num_columns):
mean[c] = np.mean(values2[:,c])
stdev[c] = np.std(values2[:,c])
episodes[c] = c
lower_curve = mean - stdev
upper_curve = mean + stdev
plt.plot(mean, label = label2)
plt.fill_between(episodes, lower_curve, upper_curve, color='r', alpha = 0.1)
if pfailgraph:
plt.ylim(0, 1)
plt.legend(loc="upper right", fontsize = fontsize)
plt.tight_layout()
save_figure(filename)
plt.clf()
#Plots two arrays with 1 std dev using seaborn
def compare_plot_CI_seaborn(values1: np.array,
label1,
values2:np.array,
label2,
xlabel,
ylabel,
filename,
ci,
pfailgraph = False):
values1_df = pd.DataFrame(values1)
values2_df = pd.DataFrame(values2)
values1_df = pd.melt(frame = values1_df,
var_name = 'Episodes',
value_name = 'runs')
values2_df = pd.melt(frame = values2_df,
var_name = 'Episodes',
value_name = 'runs')
fig, ax = plt.subplots()
sns.set(font_scale = fontscale)
ax.set_ylabel(ylabel, fontsize = fontsize)
ax.set_xlabel(xlabel, fontsize = fontsize)
sns.lineplot(ax = ax,
data = values1_df,
x = 'Episodes',
y = 'runs',
label = label1, ci = ci)
print("Completed first CI evaluation")
sns.lineplot(ax = ax,
data = values2_df,
x = 'Episodes',
y = 'runs',
label = label2, ci = ci)
print("Completed second CI evaluation")
ax.legend(loc="upper right")
if pfailgraph:
plt.ylim(0, 1)
plt.tight_layout()
save_figure(filename)
plt.clf()