티스토리 뷰

python

차트 그리기

swanB 2016. 11. 16. 08:27
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_()


공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/01   »
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
글 보관함