-
Notifications
You must be signed in to change notification settings - Fork 8
/
cpplotter.py
345 lines (281 loc) · 11.4 KB
/
cpplotter.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/usr/bin/python
"""This module generates contikipy plots."""
from __future__ import division
import os
import pickle
import numpy as np # number crunching
import matplotlib.pyplot as plt # general plotting
from matplotlib import mlab
import scipy.stats as ss
# import seaborn as sns # fancy plotting
# import pandas as pd # table manipulation
from mpl_toolkits.axes_grid1.inset_locator import inset_axes, mark_inset
# Matplotlib settings for graphs (need texlive-full, ghostscript and dvipng)
plt.rc('font', family='sans-serif', weight='bold')
# plt.rc('text', usetex=True)
# plt.rc('text.latex', preamble='\\usepackage{sfmath}')
plt.rc('xtick', labelsize=20)
plt.rc('ytick', labelsize=20)
plt.rc('axes', labelsize=20, labelweight='bold')
plt.rc('legend', fontsize=18)
plt.style.use('seaborn-deep')
# ----------------------------------------------------------------------------#
def is_string(obj):
"""Check if an object is a string."""
return all(isinstance(elem, str) for elem in obj)
# ----------------------------------------------------------------------------#
# Results compare
# ----------------------------------------------------------------------------#
def set_box_colors(bp, index):
"""Set the boxplot colors."""
color = list(plt.rcParams['axes.prop_cycle'])[index]['color']
# lw = 1.5
for box in bp['boxes']:
# change fill color
box.set(facecolor=color)
# change color the medians
for median in bp['medians']:
median.set(color='black')
# for box in bp['boxes']:
# # change outline color
# box.set(color='#7570b3', linewidth=linewidth)
# # # change fill color
# box.set(facecolor='b')
# # change color and linewidth of the whiskers
# for whisker in bp['whiskers']:
# whisker.set(color='#7570b3', linewidth=linewidth)
# # change color and linewidth of the caps
# for cap in bp['caps']:
# cap.set(color='#7570b3', linewidth=linewidth)
# # change the style of fliers and their fill
# for flier in bp['fliers']:
# flier.set(marker='o', markerfacecolor='#e7298a', alpha=0.5)
# ----------------------------------------------------------------------------#
def boxplot_zoom(ax, data, width=1, height=1,
xlim=None, ylim=None,
bp_width=0.35, pos=[1], color=1):
"""Plot a zoomed boxplot and save."""
if xlim is not None:
ax.set_xlim(xlim)
if ylim is not None:
ax.set_ylim(ylim)
axins = inset_axes(ax, width, height, loc=1)
bp = axins.boxplot(data, notch=False, positions=pos, widths=bp_width,
showfliers=False, patch_artist=True)
set_box_colors(bp, color)
axins.axis([4.7, 5.3, 310, 370])
axins.set_xticks([])
mark_inset(ax, axins,
loc1=3, loc2=4, # left and right line anchors
fc="none", # facecolor
ec="0.3", # edgecolor
ls='--')
# ----------------------------------------------------------------------------#
def set_fig_and_save(fig, ax, data, desc, dir, **kwargs):
"""Set figure properties and save as pdf."""
# get kwargs
ylim = kwargs['ylim'] if 'ylim' in kwargs else None
xlabel = kwargs['xlabel'] if 'xlabel' in kwargs else ''
ylabel = kwargs['ylabel'] if 'ylabel' in kwargs else ''
if ax is not None:
# set y limits
ax.set_ylim(ylim)
# set axis' labels
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
# Remove top axes and right axes ticks
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
# tight layout
# fig.set_tight_layout(False)
# save data for post compare
os.makedirs(dir, exist_ok=True)
if data is not None:
pickle.dump(data, open(dir + desc + '.pkl', 'wb'))
print(' ... Saving data: ' + desc + '.pkl')
# save ax for post compare
pickle.dump(ax, open(dir + 'ax_' + desc + '.pkl', 'wb+'))
print(' ... Saving pickle: ' + 'ax_' + desc + '.pkl')
# save pdf of figure plus the figure itself
fig.savefig(dir + 'fig_' + desc + '.pdf', bbox_inches="tight")
print(' ... Saving figure: ' + 'fig_' + desc + '.pdf')
# with open('myplot.pkl','rb') as fid:
# ax = pickle.load(fid)
# close all open figs
plt.close('all')
return fig, ax
# ----------------------------------------------------------------------------#
def plot_hist(df, desc, dir, x, y, ylim=None, **kwargs):
"""Plot a histogram and save."""
print(' > Plotting ' + desc + ' (HIST)')
fig, ax = plt.subplots(figsize=(8, 6))
mu = np.mean(x)
sigma = np.std(x)
# get kwargs
xlabel = kwargs['xlabel'] if 'xlabel' in kwargs else ''
ylabel = kwargs['ylabel'] if 'ylabel' in kwargs else ''
if 'color' in kwargs:
color = kwargs['color']
else:
color = list(plt.rcParams['axes.prop_cycle'])[0]['color']
# Do hist
n, bins, patches = ax.hist(x, len(x), density=True, histtype='step', cumulative=True,
stacked=True, fill=True, label=desc, color=color)
bins = np.around(np.linspace(0, max(x), len(x)), 3) # bin values to 3dp
np.insert(bins, 0, 0)
if 'Atomic' in dir:
bins = np.arange(0, max(x), 0.005)
else:
bins = np.arange(0, max(x), 0.1)
xticks = np.linspace(0, bins[len(bins)-1], 5)
ax.set_xticks(xticks)
# Add a line showing the expected distribution.
# y = ((1 / (np.sqrt(2 * np.pi) * sigma)) * np.exp(-bins**0.25))
# y = y.cumsum()
y = ss.norm.cdf(bins, mu, sigma).cumsum()
y /= y[-1]
# # Plot both
ax.plot(bins, y, 'r-', linewidth=1.5, label='Theoretical')
data = {'x': bins, 'y': y, 'errors': None,
'type': 'hist',
'xlabel': xlabel,
'ylabel': ylabel}
fig, ax = set_fig_and_save(fig, ax, data, desc, dir,
xlabel=xlabel, ylabel=ylabel)
return fig, ax
# ----------------------------------------------------------------------------#
def plot_bar(df, desc, dir, x, y, ylim=None, **kwargs):
"""Plot a barchart and save."""
print(' > Plotting ' + desc + ' (BAR)')
fig, ax = plt.subplots(figsize=(8, 6))
# constants
width = 0.35 # the width of the bars
color = list(plt.rcParams['axes.prop_cycle'])[0]['color']
# get kwargs
color = kwargs['color'] if 'color' in kwargs else color
xlabel = kwargs['xlabel'] if 'xlabel' in kwargs else ''
ylabel = kwargs['ylabel'] if 'ylabel' in kwargs else ''
stacked = kwargs['stacked'] if 'stacked' in kwargs else False
labels = kwargs['labels'] if 'labels' in kwargs else ['Retransmissions', 'Scheduled RX/TX']
ind = np.arange(len(x))
color = list(plt.rcParams['axes.prop_cycle'])[0]['color']
ax.bar(x=ind, height=y[0], width=width, color=color)
color = list(plt.rcParams['axes.prop_cycle'])[1]['color']
ax.bar(x=ind, height=y[1], width=width, color=color, bottom=y[0])
#
# ax.set_yscale('log')
# ax.set_ylim(pow(10, 1), pow(10, 5))
# if stacked:
# i = 0
# for values in y:
# print(values)
# color = list(plt.rcParams['axes.prop_cycle'])[i]['color']
# ax.bar(x=ind, height=values, width=width, color=color, bottom=y[i])
# i = i + 1
# else:
# ax.bar(x=ind, height=y, width=width, color=color)
ax.legend(labels, loc='best')
# print(xlabel)
# set x-axis
ax.set_xticks(np.arange(min(ind), max(ind)+1, 1.0))
# check for string, if not then convert x to ints for the label
if not is_string(x):
x = [int(i) for i in x]
ax.set_xticklabels(x)
# set y limits
# if ylim is not None:
# ax.set_ylim(ylim)
# plt.show()
data = {'x': x, 'y': y, 'errors': None,
'type': 'bar',
'width': width,
'xlabel': xlabel,
'ylabel': ylabel}
fig, ax = set_fig_and_save(fig, ax, data, desc, dir,
xlabel=xlabel, ylabel=ylabel)
return fig, ax
# ----------------------------------------------------------------------------#
def plot_box(df, desc, dir, x, y, ylim=None, **kwargs):
"""Plot a boxplot and save."""
print(' > Plotting ' + desc + ' (BOX)')
# subfigures
fig, ax = plt.subplots(figsize=(8, 6))
# constants
# ylim = [0, 1500]
width = 0.5 # the width of the boxes
color = list(plt.rcParams['axes.prop_cycle'])[0]['color']
# get kwargs
color = kwargs['color'] if 'color' in kwargs else color
ylim = kwargs['ylim'] if 'ylim' in kwargs else None
xlabel = kwargs['xlabel'] if 'xlabel' in kwargs else ''
ylabel = kwargs['ylabel'] if 'ylabel' in kwargs else ''
# Filter data using np.isnan
mask = ~np.isnan(y)
y = [d[m] for d, m in zip(y.T, mask.T)]
bp = ax.boxplot(y, showfliers=False, patch_artist=True)
set_box_colors(bp, 0)
data = {'x': x, 'y': y, 'errors': None,
'type': 'box',
'width': width,
'xlabel': xlabel,
'ylabel': ylabel}
fig, ax = set_fig_and_save(fig, ax, data, desc, dir,
ylim=ylim,
xlabel=xlabel,
ylabel=ylabel)
return fig, ax
# ----------------------------------------------------------------------------#
def plot_violin(df, desc, dir, x, xlabel, y, ylabel):
"""Plot a violin plot and save."""
print(' > Plotting ' + desc + ' (VIOLIN)')
fig, ax = plt.subplots(figsize=(8, 6))
xticks = [0, 1, 2, 3, 4, 5, 6]
ax.xaxis.set_ticks(xticks)
ax.violinplot(dataset=[df[df[x] == 1][y],
df[df[x] == 2][y],
df[df[x] == 3][y],
df[df[x] == 4][y]])
data = {'x': x, 'y': y, 'errors': None,
'type': 'violin',
'xlabel': xlabel,
'ylabel': ylabel}
fig, ax = set_fig_and_save(fig, ax, data, desc, dir,
xlabel=xlabel, ylabel=ylabel)
return fig, ax
# ----------------------------------------------------------------------------#
def plot_line(df, desc, dir, x, y, **kwargs):
"""Plot a line graph and save."""
print(' > Plotting ' + desc + ' (LINE)')
# constants
color = list(plt.rcParams['axes.prop_cycle'])[0]['color']
# get kwargs
errors = kwargs['errors'] if 'errors' in kwargs else None
steps = kwargs['steps'] if 'steps' in kwargs else 1
color = kwargs['color'] if 'color' in kwargs else color
marker = kwargs['marker'] if 'marker' in kwargs else 's'
ls = kwargs['ls'] if 'ls' in kwargs else '-'
lw = kwargs['lw'] if 'lw' in kwargs else 2.0
xlabel = kwargs['xlabel'] if 'xlabel' in kwargs else ''
ylabel = kwargs['ylabel'] if 'ylabel' in kwargs else ''
prefix = kwargs['prefix'] if 'prefix' in kwargs else ''
# set xticks
xticks = x
# print(steps)
# xticks = np.arange(min(), max(x)+1, steps)
if errors is not None:
# mean and std
mean = np.mean(y)
std = np.std(y)
errors = mean/np.sqrt(y)
# plot
fig, ax = plt.subplots(figsize=(8, 6))
ax.errorbar(xticks, y, errors, color=color, marker=marker, ls=ls, lw=lw)
# ax.legend(label, loc=2)
# save figure
data = {'x': x, 'y': y, 'errors': errors,
'type': 'line',
'xlabel': xlabel,
'ylabel': ylabel}
fig, ax = set_fig_and_save(fig, ax, data, prefix + desc, dir,
xlabel=xlabel, ylabel=ylabel)
return fig, ax