-
Notifications
You must be signed in to change notification settings - Fork 2
/
plot.py
executable file
·386 lines (288 loc) · 11.6 KB
/
plot.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import re,numpy,matplotlib,itertools
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import pyplot
from shared import naturalKeys
FONTSIZE = 16
def saveFigure(title,show=False,tight=True):
"function to save or show the current figure given the title"
pyplot.autoscale(enable=True, axis='both', tight=tight)
if show:
pyplot.show()
pyplot.close()
return None
else:
title = re.subn(r"[\s$]","_",title)[0]
title = re.subn(r"[^\w\d_]","",title)[0]
filename = "figures/{}.png".format(title.lower())
pyplot.savefig("report/"+filename)
pyplot.close("all")
return filename
def histogram(data,title="",xLabel="Value",yLabel="Frequency",lines=None,numberBars=10,show=False):
"plots a histogram of the given data data"
maxFrequency = len(data)/numberBars
pyplot.figure(num=title)
pyplot.hold(True)
pyplot.hist(data,numberBars)
#pyplot.title(title,fontsize=FONTSIZE)
pyplot.xlabel(xLabel,fontsize=FONTSIZE)
pyplot.ylabel(yLabel,fontsize=FONTSIZE)
if lines:
for name,line in sorted(lines.items(),key=lambda x:naturalKeys(x[0]),reverse=True):
pyplot.plot([line,line],[0,maxFrequency],label=name,linewidth=1)
pyplot.legend()
pyplot.hold(False)
return saveFigure(title,show)
def bar(labels,values,title="",xLabel="",yLabel="",lines=False,show=False):
"plots a bar chart with the givne labels and data values"
def plot9():
data = [ ("data1", 34), ("data2", 22),
("data3", 11), ( "data4", 28),
("data5", 57), ( "data6", 39),
("data7", 23), ( "data8", 98)]
N = len( data )
x = np.arange(1, N+1)
y = [ num for (s, num) in data ]
labels = [ s for (s, num) in data ]
width = 1
bar1 = plt.bar( x, y, width, color="y" )
plt.ylabel( 'Intensity' )
plt.xticks(x + width/2.0, labels )
plt.show()
length = len(values)
indexs = numpy.arange(length)
barWidth = 1
figure = pyplot.figure(num=title)
axes = pyplot.axes()
pyplot.hold(True)
barAxes = pyplot.bar(indexs,values,barWidth)
pyplot.legend(barAxes,labels,fontsize=FONTSIZE)
#pyplot.title(title,fontsize=FONTSIZE)
labels = [label[:10] for label in labels]
pyplot.xticks(indexs + barWidth/2, labels, rotation=0)
pyplot.yticks(range(0,max(values)+10,5))
pyplot.xlabel(xLabel,fontsize=FONTSIZE)
pyplot.ylabel(yLabel,fontsize=FONTSIZE)
if lines:
for name,line in sorted(lines.items(),key=lambda x:naturalKeys(x[0]),reverse=True):
pyplot.plot([line,line],[0,maxFrequency],label=name,linewidth=1)
pyplot.legend()
pyplot.hold(False)
return saveFigure(title,show)
def pie(labels,values,title="",xLabel="",yLabel="",show=False):
"plot a pie cahrt with the given labels and data values"
figure = pyplot.figure(num=title)
axes = pyplot.axes()
pyplot.pie(values, labels=[label[:10] for label in labels])
pyplot.legend(labels,fontsize=FONTSIZE)
#pyplot.title(title,fontsize=FONTSIZE)
pyplot.xlabel(xLabel,fontsize=FONTSIZE)
pyplot.ylabel(yLabel,fontsize=FONTSIZE)
return saveFigure(title,show)
def line(xAxis,yAxis,title="",xLabel="x",yLabel="y",location=4,scatter=None,xTicks=None,yTicks=None,tight=True,show=False):
"method to plot the class data and if DEBUG = False save the figure"
pyplot.figure(num=title)
pyplot.hold(True)
#pyplot.title(title)
if (type(xAxis[0]) == str):
xTicks = xAxis
xAxis = numpy.arange(len(xAxis))
if (type(yAxis) == dict):
yMin,yMax = 0,0
for name,data in sorted(yAxis.items(),key=lambda x:naturalKeys(x[0])):
yMin = min(data+[yMin])
yMax = max(data+[yMax])
pyplot.plot(xAxis[:len(data)],data[:len(xAxis)],label=name)
pyplot.legend(fontsize=FONTSIZE,loc=location)
else:
yMin = min(yAxis)
yMax = max(yAxis)
pyplot.plot(xAxis[:len(yAxis)],yAxis[:len(xAxis)],label="")
if xTicks:
pyplot.xticks(xAxis, xTicks, rotation=0)
if scatter:
for name,data in scatter.items():
pyplot.scatter(data[0],data[1],label=name)
pyplot.xlabel(xLabel,fontsize=FONTSIZE)
pyplot.ylabel(yLabel,fontsize=FONTSIZE)
ySpace = (yMax-yMin)*0.05
pyplot.ylim((yMin-ySpace,yMax+ySpace))
pyplot.hold(False)
return saveFigure(title,show,tight)
def line3(xAxis,yAxis,zAxis,title="",xLabel="x",yLabel="y",zLabel="z",show=False):
"method to plot the class data and if DEBUG = False save the figure"
matplotlib.rcParams['legend.fontsize'] = 10
figure = pyplot.figure(num=title)
axes = Axes3D(figure)
# axes.set_aspect('equal')
axes.plot(xAxis, yAxis, zAxis, label=title)
#scaleBox(axes,xAxis, yAxis, zAxis)
axes.set_xlabel(xLabel,fontsize=FONTSIZE)
axes.set_ylabel(yLabel,fontsize=FONTSIZE)
axes.set_zlabel(zLabel,fontsize=FONTSIZE)
#axes.set_title(title,fontsize=FONTSIZE)
axes.legend()
return saveFigure(title,show)
def path(paths,title="",xLabel="x",yLabel="y",arrows=None,show=False,tight=True):
"function to plot the given paths"
figure = pyplot.figure(num=title)
axes = pyplot.axes()
pyplot.hold(True)
#pyplot.title(title)
for label,data in sorted(paths.items(),key=lambda x:naturalKeys(x[0])):
if len(data) == 3:
[x,y,z] = data
else:
[x,y] = data
axes.plot(x[:len(y)],y[:len(x)],label=label)
if arrows:
for label,data in arrows.items():
[x1,y1,x2,y2] = data
axes.arrow(x1,y1,x2,y2,head_width=0.1, fc='k', ec='k')
axes.set_xlabel(xLabel,fontsize=FONTSIZE)
axes.set_ylabel(yLabel,fontsize=FONTSIZE)
#axes.set_title(title,fontsize=FONTSIZE)
axes.legend(loc=4)
pyplot.hold(False)
pyplot.axis('equal')
return saveFigure(title,show,tight)
def path2(paths,title="",xLabel="x",yLabel="y",scatter=None,show=False):
"function to plot the given paths"
figure = pyplot.figure(num=title)
axes = pyplot.axes()
pyplot.hold(True)
#pyplot.title(title)
for label,data in sorted(paths.items(),key=lambda x:naturalKeys(x[0])):
[x,y] = data
axes.plot(x[:len(y)],y[:len(x)],label=label)
if scatter:
for name,data in sorted(scatter.items(),key=lambda x:naturalKeys(x[0])):
pyplot.scatter(data[0],data[1],label=name)
axes.set_xlabel(xLabel,fontsize=FONTSIZE)
axes.set_ylabel(yLabel,fontsize=FONTSIZE)
#axes.set_title(title,fontsize=FONTSIZE)
axes.legend(loc=4)
pyplot.hold(False)
return saveFigure(title,show)
def path3(paths,title="",xLabel="x",yLabel="y",zLabel="z",show=False):
"method to plot the class data and if DEBUG = False save the figure"
matplotlib.rcParams['legend.fontsize'] = 10
figure = pyplot.figure(num=title)
pyplot.hold(True)
axes = Axes3D(figure)
#axes.set_aspect('equal')
for label,data in paths.items():
[x,y,z] = data
axes.plot(x, y, z, label=label)
#scaleBox(axes,x, y, z)
pyplot.hold(False)
axes.set_xlabel(xLabel,fontsize=FONTSIZE)
axes.set_ylabel(yLabel,fontsize=FONTSIZE)
axes.set_zlabel(zLabel,fontsize=FONTSIZE)
#axes.set_title(title,fontsize=FONTSIZE)
axes.legend()
return saveFigure(title,show)
def scatter(xAxis,yAxis,title="",xLabel="x",yLabel="y",xTicks=None,yTicks=None,lines=None,show=False):
"method to plot the class data and if DEBUG = False save the figure"
pyplot.figure(num=title)
axes = pyplot.axes()
pyplot.hold(True)
#pyplot.title(title,fontsize=FONTSIZE)
maxValue = 0
minValue = numpy.infty
if (type(xAxis[0]) == str):
xTicks = xAxis
xAxis = numpy.arange(len(xAxis))
if (type(yAxis) == dict):
colours = itertools.cycle(["b","g","r","c","m","y","k"])
for name,data in sorted(yAxis.items(),key=lambda x:naturalKeys(x[0])):
minValue = min(data+[minValue])
maxValue = max(data+[maxValue])
axes.scatter(xAxis[:len(data)],data[:len(xAxis)],label=name,color=next(colours))
handles, labels = axes.get_legend_handles_labels()
l1 = axes.legend(handles,labels,loc=4,fontsize=FONTSIZE)
else:
axes.scatter(xAxis[:len(yAxis)],yAxis[:len(xAxis)],label="")
if xTicks:
axes.xticks(xAxis, xTicks, rotation=0)
axes.set_xlabel(xLabel,fontsize=FONTSIZE)
axes.set_ylabel(yLabel,fontsize=FONTSIZE)
#axes.set_title(title,fontsize=FONTSIZE)
if lines:
for name,line in sorted(lines.items(),key=lambda x:naturalKeys(x[0])):
axes.plot(line[0],line[1],label=name,linewidth=1)
handles2, labels2 = axes.get_legend_handles_labels()
for i in range(len(handles)):
handles2.remove(handles[i])
labels2.remove(labels[i])
l2 = axes.legend(handles2,labels2,loc=2,fontsize=FONTSIZE)
pyplot.gca().add_artist(l1)
pyplot.hold(False)
return saveFigure(title,show)
def scatter2(series,title="",xLabel="x",yLabel="y",xTicks=None,yTicks=None,lines=None,location=4,show=False):
"method to plot the class data and if DEBUG = False save the figure"
pyplot.figure(num=title)
axes = pyplot.axes()
pyplot.hold(True)
#pyplot.title(title,fontsize=FONTSIZE)
colours = itertools.cycle(["b","g","r","c","m","y","k"])
for name,data in sorted(series.items(),key=lambda x:naturalKeys(x[0])):
axes.scatter(data[0],data[1],label=name,color=next(colours))
for name,data in sorted(lines.items(),key=lambda x:naturalKeys(x[0])):
axes.plot(data[0],data[1],label=name,color=next(colours))
axes.set_xlabel(xLabel,fontsize=FONTSIZE)
axes.set_ylabel(yLabel,fontsize=FONTSIZE)
#axes.set_title(title,fontsize=FONTSIZE)
axes.legend(fontsize=FONTSIZE,loc=location)
pyplot.hold(False)
return saveFigure(title,show)
def scatter3(xAxis,yAxis,zAxis,title="",xLabel="x",yLabel="y",zLabel="z",scaleBox=False,show=False):
"function to plot a 3d scatter plot"
matplotlib.rcParams['legend.fontsize'] = FONTSIZE
figure = pyplot.figure(num=title)
axes = Axes3D(figure)
#axes.set_aspect('equal')
if (type(zAxis) == dict):
colours = itertools.cycle(["b","g","r","c","m","y","k"])
for name,data in sorted(zAxis.items(),key=lambda x:naturalKeys(x[0])):
axes.scatter(xAxis, yAxis, data, label=name, color=next(colours))
axes.legend(fontsize=FONTSIZE)
else:
axes.scatter(xAxis, yAxis, zAxis, label=title)
if scaleBox:
addScaleBox(axes,xAxis, yAxis, zAxis)
axes.set_xlabel(xLabel,fontsize=FONTSIZE)
axes.set_ylabel(yLabel,fontsize=FONTSIZE)
axes.set_zlabel(zLabel,fontsize=FONTSIZE)
#axes.set_title(title,fontsize=FONTSIZE)
return saveFigure(title,show)
def surface(xArray,yArray,zArray,title="",xLabel="x",yLabel="y",zLabel="z",show=False):
"function to plot a surface plot"
matplotlib.rcParams['legend.fontsize'] = FONTSIZE
figure = pyplot.figure(num=title)
axes = Axes3D(figure)
axes.plot_surface(xArray,yArray,zArray)
axes.set_xlabel(xLabel,fontsize=FONTSIZE)
axes.set_ylabel(yLabel,fontsize=FONTSIZE)
axes.set_zlabel(zLabel,fontsize=FONTSIZE)
#axes.set_title(title,fontsize=FONTSIZE)
return saveFigure(title,show)
def addScaleBox(axes,xAxis,yAxis,zAxis):
"plots a invisible scale box to correct the aspect ratio"
pyplot.hold(True)
# Create cubic bounding box to simulate equal aspect ratio
max_range = numpy.array([max(xAxis)-min(xAxis), max(yAxis)-min(yAxis), max(zAxis)-min(zAxis)]).max()
Xb = 0.5*max_range*numpy.mgrid[-1:2:2,-1:2:2,-1:2:2][0].flatten() + 0.5*(max(xAxis)+min(xAxis))
Yb = 0.5*max_range*numpy.mgrid[-1:2:2,-1:2:2,-1:2:2][1].flatten() + 0.5*(max(yAxis)+min(yAxis))
Zb = 0.5*max_range*numpy.mgrid[-1:2:2,-1:2:2,-1:2:2][2].flatten() + 0.5*(max(zAxis)+min(zAxis))
# Comment or uncomment following both lines to test the fake bounding box:
for xb, yb, zb in zip(Xb, Yb, Zb):
axes.plot([xb], [yb], [zb], 'w')
pyplot.hold(False)
if (__name__ == "__main__"):
pyplot.close("all")
histogram([2,4,5,6.7])
histogram(numpy.random.randn(2000)*8)
histogram(numpy.random.randn(2000)*8,lines=[2,3],show=True)
scatter([0,1,2],[0,1,2])
scatter3([0,1,2,8],[0,1,2,3],[0,1,2,4])
scatter("hello",{"point1":(1,2),"point2":(1,4)," ":(2,4)})