-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlotWindow.py
160 lines (128 loc) · 5.15 KB
/
PlotWindow.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
import sys
from PyQt4 import QtGui, QtCore
import numpy
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
import matplotlib.pyplot as plt
import GlobalParameters
class PlotWindow(QtGui.QDialog):
def __init__(self, title, parent=None):
super(PlotWindow, self).__init__(parent)
# a figure instance to plot on
self.figure = plt.figure()
self.setWindowTitle(title)
# this is the Canvas Widget that displays the `figure`
# it takes the `figure` instance as a parameter to __init__
self.canvas = FigureCanvas(self.figure)
# this is the Navigation widget
# it takes the Canvas widget and a parent
self.toolbar = NavigationToolbar(self.canvas, self)
# set the layout
layout = QtGui.QVBoxLayout()
layout.addWidget(self.toolbar)
layout.addWidget(self.canvas)
self.setLayout(layout)
self.y = []
self.x = []
self.maxY = 0
# create an axis
self.ax = plt.subplot()
self.isDraw = False
def DrawPlot(self, NNR):
''' plot some random stuff '''
# random data
self.y.append(float(NNR))
self.x.append(len(self.y))
self.ax.clear()
# remeber the old graph
self.ax.hold(True)
self.ax.plot(self.x, self.y, 'o')
self.canvas.draw()
self.ax.set_ylabel('Nearest Neighbor Ratio')
self.ax.set_xlabel('Time')
try:
coefficients = numpy.polyfit(self.x, self.y, 3)
polynomial = numpy.poly1d(coefficients)
ys = polynomial(self.x)
print coefficients
GlobalParameters.RegressionCoefficients = coefficients
print polynomial
self.ax.plot(self.x, ys)
pstr = r'$' + str(coefficients[0]) + r'x^{3}' + str(coefficients[1]) + r'x^{2}' + str(
coefficients[2]) + r'x +' + str(coefficients[3]) + r'$'
# ax.annotate(pstr,
# xy=(1, 1), xycoords='axes fraction',
# xytext=(-300, -50), textcoords='offset points', fontsize=10)
self.canvas.draw()
except:
pass
def DrawPlot2(self):
if len(self.x) == 0:
for i in range(len(self.y)):
self.x.append(i)
self.ax.clear()
# remeber the old graph
self.ax.hold(True)
self.ax.plot(self.x, self.y, 'o')
self.canvas.draw()
self.ax.set_ylabel('Time Nearest Neighbor Ratio')
self.ax.set_xlabel('Tweets')
try:
coefficients = numpy.polyfit(self.x, self.y, 3)
polynomial = numpy.poly1d(coefficients)
ys = polynomial(self.x)
print coefficients
print polynomial
self.ax.plot(self.x, ys)
pstr = r'$' + str(coefficients[0]) + r'x^{3}' + str(coefficients[1]) + r'x^{2}' + str(
coefficients[2]) + r'x +' + str(coefficients[3]) + r'$'
# ax.annotate(pstr,
# xy=(1, 1), xycoords='axes fraction',
# xytext=(-300, -50), textcoords='offset points', fontsize=10)
self.canvas.draw()
except:
pass
def DrawPlot3(self):
if len(self.x) == 0:
for i in range(len(self.y)):
self.x.append(i)
self.ax.clear()
# remeber the old graph
self.ax.hold(True)
self.ax.plot(self.x, self.y)
self.ax.plot(self.x, self.y, 'go')
self.canvas.draw()
self.ax.xaxis.set_ticks(numpy.arange(0, len(self.x), 1))
#self.ax.set_xlim(1, len(self.x), 1) # set axis limits
#self.ax.ylim(0, 30.)
maxy = max(self.y)
#print "MAXY", maxy
self.maxY = (5 - maxy % 5) + maxy
#print "MAXY", maxy
self.ax.set_ylim(0, self.maxY)
self.ax.set_ylabel('# Active Nodes')
self.ax.set_xlabel('Step')
def DrawPlot4(self):
self.x = numpy.arange(len(self.y))
self.ax.clear()
# remeber the old graph
self.ax.hold(True)
width = 0.35
rects = self.ax.bar(self.x, self.y, width, color='r')
self.canvas.draw()
self.ax.set_xticks(self.x + width / 2)
#self.ax.set_xticklabels(("1","1","1","1","1"))
self.ax.set_xticklabels(numpy.arange(0, len(self.x), 1))
#self.ax.xaxis.set_ticks(numpy.arange(0, len(self.x), 1))
#self.ax.set_xlim(1, len(self.x), 1) # set axis limits
#self.ax.ylim(0, 30.)
#print "MAXY", maxy
self.maxY = 100
self.ax.set_ylim(0, self.maxY)
self.ax.set_ylabel('Percentage of active cities(%)')
self.ax.set_xlabel('Step')
for rect in rects:
height = rect.get_height()
self.ax.text(rect.get_x() + rect.get_width() / 2., height + 2,
'%d' % int(height),
ha='center', va='bottom')