-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEmbeddedMatplotlibWindow.py
More file actions
187 lines (138 loc) · 4.92 KB
/
EmbeddedMatplotlibWindow.py
File metadata and controls
187 lines (138 loc) · 4.92 KB
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
import sys
from PyQt5.QtWidgets import QDialog, QApplication, QPushButton, QVBoxLayout, QHBoxLayout, QWidget
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
import matplotlib.pyplot as plt
import matplotlib.ticker as plticker
import datetime
import random
from Application import Application
import numpy as np
import pandas as pd
class PlottingWindow(QDialog):
def __init__(self, App, parent=None):
super(PlottingWindow, self).__init__(parent)
self.app = App
# A figure instance to plot on
self.figure = plt.figure()
# 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)
# Plot pie chart button
self.plotPieBtn = QPushButton('Pie Chart')
self.plotPieBtn.clicked.connect(self.plotPie)
# Plot bar graph button
self.plotBarBtn = QPushButton('Bar Graph')
self.plotBarBtn.clicked.connect(self.plotBar)
# Plot time series button
self.plotTimeSeriesBtn = QPushButton('Time Series')
self.plotTimeSeriesBtn.clicked.connect(self.plotTimeSeries)
# Set the plottingWindow layout
layout = QVBoxLayout()
layout.addWidget(self.toolbar)
layout.addWidget(self.canvas)
# Within the layout, create horizontal
# layout for various plotting functionality
btnLayout = QHBoxLayout()
layout.addLayout(btnLayout)
btnLayout.addWidget(self.plotPieBtn)
btnLayout.addWidget(self.plotBarBtn)
btnLayout.addWidget(self.plotTimeSeriesBtn)
self.setLayout(layout)
def plotPie(self):
# Get data to plot
cats = self.app.getCategoryNamesList()[1:]
data = [self.app.getAmountSpentByCategory(c) for c in cats]
self.figure.clear()
self.ax = self.figure.add_subplot(111)
self.ax.clear()
self.ax.pie(data, labels=cats)
self.ax.set_title("Spending by Category")
self.canvas.draw()
def plotBar(self):
# Get data to plot
cats = self.app.getCategoryNamesList()[1:]
n_groups = len(cats)
amountSpent = [self.app.getAmountSpentByCategory(c) for c in cats]
amountAllotted = [self.app.getAmountAllottedByCategory(c) for c in cats]
self.figure.clear()
self.ax = self.figure.add_subplot(111)
self.ax.clear()\
# Format preparation
index = np.arange(n_groups)
bar_width = 0.35
opacity = 0.8
rects1 = plt.bar(index, amountAllotted, bar_width,
alpha=opacity,
color='b',
label='Allotted')
rects2 = plt.bar(index + bar_width, amountSpent, bar_width,
alpha=opacity,
color='g',
label='Spent')
plt.xlabel('Spending')
plt.ylabel('Category')
plt.title('Allotment and Spending')
plt.xticks(index + bar_width, [x for x in self.app.getCategoryNamesList()[1:]])
plt.xticks(rotation=40)
plt.legend()
plt.tight_layout()
self.canvas.draw()
def plotTimeSeries(self):
# Get data to plot
spendingByDay, listOfDates = self.app.getTimeSeriesData()
# Prepare figure
self.figure.clear()
self.ax = self.figure.add_subplot(111)
self.ax.clear()
plt.xlabel('Time')
plt.ylabel('Total Spending')
plt.title('Charges Over Time')
plt.tight_layout()
plt.xticks(rotation=60)
plt.plot(listOfDates, spendingByDay)
self.canvas.draw()
class ProjectionWidget(QDialog):
def __init__(self, App, parent=None):
super(ProjectionWidget, self).__init__(parent)
self.app = App
# A figure instance to plot on
self.figure = plt.figure()
# 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 plottingWindow layout
layout = QVBoxLayout()
layout.addWidget(self.toolbar)
layout.addWidget(self.canvas)
# Within the layout, create horizontal
# layout for various plotting functionality
btnLayout = QHBoxLayout()
layout.addLayout(btnLayout)
# btnLayout.addWidget(self.plot3MonthProjectionBtn)
self.setLayout(layout)
self.plotProjection()
def plotProjection(self):
dates, runningBalance = self.app.getProjectionData()
# Prepare figure
self.figure.clear()
self.ax = self.figure.add_subplot(111)
self.ax.clear()
plt.xlabel('Time')
plt.ylabel('Checking Account Balance')
plt.title('Cash Projection')
plt.tight_layout()
plt.xticks(rotation=60)
plt.plot(dates, runningBalance)
self.canvas.draw()
if __name__ == '__main__':
app = QApplication(sys.argv)
plotWindow = PlottingWindow()
plotWindow.show()
sys.exit(app.exec_())