Skip to content

Instantly share code, notes, and snippets.

@eyllanesc
Created December 25, 2017 01:02
Show Gist options
  • Save eyllanesc/7f951debc8bac2c591a25a2982376397 to your computer and use it in GitHub Desktop.
Save eyllanesc/7f951debc8bac2c591a25a2982376397 to your computer and use it in GitHub Desktop.
from PyQt5 import QtGui,QtWidgets
import sys
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import style
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
style.use('ggplot')
class PrettyWidget(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
#super(PrettyWidget, self).__init__()
self.initUI()
def initUI(self):
self.setGeometry(600,300, 1000, 600)
self.center()
self.setWindowTitle('Revision on Plots, Tables and File Browser')
#Grid Layout
grid = QtWidgets.QGridLayout()
widget = QtWidgets.QWidget(self)
self.setCentralWidget(widget)
widget.setLayout(grid)
#Import CSV Button
btn1 = QtWidgets.QPushButton('Import CSV', self)
btn1.resize(btn1.sizeHint())
btn1.clicked.connect(self.getCSV)
grid.addWidget(btn1, 1, 0)
#Canvas and Toolbar
self.figure = plt.figure(figsize=(15,5))
self.canvas = FigureCanvas(self.figure)
grid.addWidget(self.canvas, 2,0,1,2)
#DropDown mean / comboBox
self.df = pd.DataFrame()
self.rating_list = []
self.yq_list = []
self.comboBox = QtWidgets.QComboBox(self)
self.comboBox.addItems(self.rating_list)
grid.addWidget(self.comboBox, 0, 0)
self.comboBox2 = QtWidgets.QComboBox(self)
self.comboBox2.addItems(self.yq_list)
grid.addWidget(self.comboBox2, 0, 1)
#Plot Button
btn2 = QtWidgets.QPushButton('Plot', self)
btn2.resize(btn2.sizeHint())
btn2.clicked.connect(self.plot)
grid.addWidget(btn2, 1, 1)
self.show()
def getCSV(self):
filePath, _ = QtWidgets.QFileDialog.getOpenFileName(self, 'Open file', '/home')
if filePath != "":
print (filePath)
self.df = pd.read_csv(str(filePath))
self.rating_list = self.df.rating.unique().tolist()
self.yq_list = [str(x) for x in self.df.yq.unique().tolist()]
self.comboBox.addItems(self.rating_list)
self.comboBox2.addItems(self.yq_list)
print (self.rating_list)
def plot(self):
y = []
for n in range(3):
try:
y.append(self.table.item(0, n).text())
except:
y.append(np.nan)
p1 = self.df.ix[(self.df.rating == str(self.comboBox.currentText())) & (self.df.yq == int(str(self.comboBox2.currentText()))), :]
print (p1)
plt.cla()
ax = self.figure.add_subplot(111)
ax.plot(p1.ix[:, 0], 'g', label = "Pred on data with Model")
ax.plot(p1.ix[:, 1], label = "adj Pred to non-decreasing")
ax.plot(p1.ix[:, 3], label = "Fitting value in Model")
ax.plot(p1.ix[:, 2], 'r', label = "Actual PD")
ax.plot(p1.ix[:, 4], 'y', label = "Long Run Avg")
ax.set_title('Canada C&I PD Plot')
ax.legend(loc = 0)
self.canvas.draw()
def center(self):
qr = self.frameGeometry()
cp = QtWidgets.QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
def main():
app = QtWidgets.QApplication(sys.argv)
w = PrettyWidget()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
@yyykkkyyy
Copy link

Hi,
I'm from StackOverFlow's question,
I tried with this code and I get a following error.

AttributeError: 'DataFrame' object has no attribute 'rating'

This error occurred at line 70, in getCSV.
self.rating_list = self.df.rating.unique().tolist()

How can I fix this error ?

Thanks,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment