使用python从数据库中获得烛台图

时间:2018-10-18 10:57:40

标签: database python-3.x candlestick-chart

import datetime as dt
import matplotlib.pyplot as plt
from matplotlib import style
import pandas as pd
import pandas_datareader.data as web
import numpy as np
import MySQLdb as db

这部分用于连接数据库并从数据库中检索数据

db = db.connect(host="localhost", user="root", passwd="root", db="toroi")
cur=db.cursor()
cur.execute('SELECT DATE,Volume,OPEN,High,Low,CLOSE,Adj_Close FROM btc_csv')
fig=plt.figure()
graph=fig.set_facecolor('white')
dates=[]
volume=[]
price=[]
openh=[]
high=[]
low=[]
adj_close=[]


for row in cur.fetchall():
DATE=str(row[0])
print("the total data is ",row)

print("the total data is ",row)
Volume=str(row[1])
print("the total  data ",row)

Open=str(row[2])

High=str(row[3])

Low=str(row[4])

CLOSE=str(row[5])

Adj_Close=str(row[6])




dates.append(DATE)
volume.append(Volume)
openh.append(Open)
high.append(High)
low.append(Low)

price.append(CLOSE)
adj_close.append(Adj_Close)


print(dates)
print(volume)
print(openh)
print(high)
print(low)
print(price)
print(adj_close)
openh=np.array([openh])
high=np.array([high])
low=np.array([low])
close=np.array([price])
volume=np.array([volume])

这里的代码很好,但是由于我无法绘制烛台图,所以我做了所有的事情

n_groups=len(openh)
index=np.arange(n_groups)
fig,ax=plt.subplots()
bar_height=np.absolute(openh-close)
bat_bottom=np.minimum(openh,close)
yerr_high=high - np.maximum(openh,close)
yerr_low=np.minimun(open,close)-low+bar_height
bar_width=0.5
opacity=1
error_config={'alpha':opacity}
rects1 = ax.bar(index,bar_height,bar_width,bar_bottom,alpha=opacity,error_kw = error_config,yerr =[yerr_low, yerr_high])
ax.set_xlabel('Date')
Text(0.5,0,'Date')
ax.set_ylabel('Price')
Text(0,0.5,'Price')
ax.set_title('Candlestick Chart')
Text(0.5,1,'Candlestick Chart')


plt.show()
cur.close()
db.close() 

请帮助我修改代码或帮助我重新编写代码以读取数据表单数据库以绘制烛台图 预先感谢

0 个答案:

没有答案