-
Notifications
You must be signed in to change notification settings - Fork 4
/
visualize.py
195 lines (159 loc) · 5.62 KB
/
visualize.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
import numpy
import matplotlib
import matplotlib.pyplot as pyplot
from matplotlib.backends.backend_pdf import PdfPages
import seaborn
#
# changing font size
seaborn.set_context("poster", font_scale=1.7, rc={'font.size': 32,
# 'axes.labelsize': fontSize,
# 'xtick.labelsize': fontSize,
# 'ytick.labelsize': fontSize,
# 'legend.fontsize': fontSize,
'text.usetex': True
})
# matplotlib.rcParams.update({'font.size': 22})
def beautify_with_seaborn():
#
seaborn.set_style('white')
seaborn.despine(trim=True)
seaborn.set_context('poster')
def visualize_curves(curves,
output=None,
labels=None,
lines=None,
linestyles=None,
linewidths=None,
palette='hls',
markers=None,
loc=None):
"""
WRITEME
"""
seaborn.set_style('white')
# seaborn.set_context('poster')
n_curves = len(curves)
n_lines = len(lines)
#
# default legend location, upper right
if loc is None:
loc = 3
#
# setting the palette
seaborn.set_palette(palette, n_colors=(n_curves + n_lines))
#
# default linestyle
default_linestyle = '-'
if linestyles is None:
linestyles = [default_linestyle for i in range(n_curves)]
default_width = 5
if linewidths is None:
linewidths = [default_width for i in range(n_curves)]
if markers is None:
markers = ['o', 'v', '1', '2', '3']
for i, curve in enumerate(curves):
curve_x, curve_y = curve
if labels is not None:
label = labels[i]
line = pyplot.plot(curve_x, curve_y,
label=label,
linestyle=linestyles[i],
linewidth=linewidths[i],
# marker=markers[i]
)
else:
line = pyplot.plot(curve_x, curve_y,
linestyle=linestyles[i],
linewidth=linewidths[i],
# marker=markers[i]
)
#
# lastly plotting straight lines, if present
if lines is not None:
default_linestyle = '--'
for i, line_y in enumerate(lines):
#
# this feels a little bit hackish, assuming all share the same axis
prototypical_x_axis = curves[0][0]
start_x = prototypical_x_axis[0]
end_x = prototypical_x_axis[-1]
pyplot.plot([start_x, end_x],
[line_y, line_y],
linestyle=default_linestyle,
linewidth=default_width) # linestyles[i + n_curves])
#
# setting up the legend
if labels is not None:
legend = pyplot.legend(labels, loc=loc)
seaborn.despine()
if output is not None:
fig = pyplot.gcf()
fig_width = 18.5
fig_height = 10.5
dpi = 100
fig.set_size_inches(fig_width, fig_height)
fig.savefig(output,
# additional_artists=[legend],
dpi=dpi,
bbox_inches='tight')
pyplot.close(fig)
else:
#
# shall this be mutually exclusive with file saving?
pyplot.show()
DATASET_LIST = ['nltcs', 'msnbc', 'kdd',
'plants', 'baudio', 'jester', 'bnetflix',
'accidents', 'tretail', 'pumsb_star',
'dna', 'kosarek', 'msweb',
'book', 'tmovie', 'cwebkb',
'cr52', 'c20ng', 'bbc', 'ad']
def visualize_histograms(histograms,
output=None,
labels=DATASET_LIST,
linestyles=None,
rotation=90,
legend=None,
y_log=False,
colors=['seagreen', 'orange', 'cornflowerblue']):
"""
Plotting histograms one near the other
"""
n_histograms = len(histograms)
#
# assuming homogeneous data leengths
# TODO: better error checking
n_ticks = len(histograms[0])
bin_width = 1 / (n_histograms + 1)
bins = [[i + j * bin_width for i in range(n_ticks)]
for j in range(1, n_histograms + 1)]
#
# setting up seaborn
seaborn.set_style("white")
seaborn.set_context("poster")
# seaborn.set_palette(palette, n_colors=n_histograms)
fig, ax = pyplot.subplots()
if legend is not None:
_legend = pyplot.legend(legend)
#
# setting labels
middle_histogram = n_histograms // 2 + 1 # if n_histograms > 1 else 0
pyplot.xticks(bins[middle_histogram], DATASET_LIST)
if rotation is not None:
locs, labels = pyplot.xticks()
pyplot.setp(labels, rotation=90)
#
# actual plotting
print(histograms)
for i, histogram in enumerate(histograms):
ax.bar(bins[i], histogram, width=bin_width,
facecolor=colors[i], edgecolor="none",
log=y_log)
seaborn.despine()
if output is not None:
pp = PdfPages(output)
pp.savefig(fig)
pp.close()
if __name__ == '__main__':
labels = [i for i in range(-10, 10)]
points = [numpy.exp(i) for i in labels]
visualize_curves([(labels, points)], labels=['a', 'b'])