import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
import pandas_datareader.data as web
class MyWindow(QWidget):
def __init__(self):
super().__init__()
self.setupUI()
def setupUI(self):
self.setGeometry(600,200, 1200, 600)
self.setWindowTitle("PyChart Viewr v0.1")
self.setWindowIcon(QIcon('chart_stock.png'))
self.lineEdit = QLineEdit()
self.pushButton = QPushButton("차트그리기")
self.pushButton.clicked.connect(self.pushButtonClicked)
self.fig = plt.Figure()
self.canvas = FigureCanvas(self.fig)
leftLayout = QVBoxLayout()
leftLayout.addWidget(self.canvas)
rightLayout = QVBoxLayout()
rightLayout.addWidget(self.lineEdit)
rightLayout.addWidget(self.pushButton)
rightLayout.addStretch(1)
layout = QHBoxLayout()
layout.addLayout(leftLayout)
layout.addLayout(rightLayout)
layout.setStretchFactor(leftLayout,1)
layout.setStretchFactor(rightLayout,0)
self.setLayout(layout)
self.show()
def pushButtonClicked(self):
code = self.lineEdit.text()
df = web.DataReader(code, "yahoo")
df['MA20'] = df['Adj Close'].rolling(window=20).mean()
df['MA60'] = df['Adj Close'].rolling(window=60).mean()
ax = self.fig.add_subplot(111)
ax.plot(df.index, df['Adj Close'], label='Adj Close')
ax.plot(df.index, df['MA20'], label='MA20')
ax.plot(df.index, df['MA60'], label='MA60')
ax.legend(loc='upper right')
ax.grid()
self.canvas.draw()
if __name__ == "__main__":
app = QApplication(sys.argv)
mywindow = MyWindow()
app.exec_()