forked from foprea/Jeopy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlotRenderer.py
62 lines (51 loc) · 1.93 KB
/
PlotRenderer.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
"""
Small helper thread to render the final plot using Matplotlib.
Emits finishedPlot signal with a string to the path where the file has been written to.
"""
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import matplotlib.pyplot as plt
import tempfile
class PlotRenderer(QThread):
finishedPlot = pyqtSignal(str)
"""
lets the gui know where the plot has been drawn
"""
def __init__(self, scores, filePath, drawLegend, dpi, parent):
"""
@param scores: the names, scores and colors of every player
@type scores: dictionary (keys are player names)
@param filePath: where to save the drawn plot
@type filePath: str
@param drawLegend: whether to draw a legend or not, useful for saving as PNG
@type drawLegend: bool
@param dpi: dots per inch for render, the higher it is, the more time it takes
to draw, default value set in Gui.py is enough to get 800x600, should ideally
be calculated based on canvas resolution
@type dpi: int
"""
super(PlotRenderer, self).__init__(parent)
self.scores = scores
self.filePath = filePath
self.drawLegend = drawLegend
self.dpi = dpi
def run(self):
"""
every thread overwrites run()
draws line plots for each player with a different color
also draws a legend if required
"""
scores = self.scores
print scores
fig = plt.figure()
ax = fig.add_subplot(111)
plt.hold(True)
for player in scores.items():
x = range(1, len(player[1][0]) + 1)
ax.plot(x, player[1][0], color = player[1][1], label = player[0])
if self.drawLegend:
ax.legend(loc = 'best')
#linePlotPath = tempfile.mkstemp(suffix = '.png', dir = path)[1]
plt.savefig(self.filePath, dpi = self.dpi)
self.finishedPlot.emit(self.filePath)
self.exec_()