以及pandas使用dataframe读取数据库数据及绘图案例
import mysql.connector
import pandas as pd
import matplotlib.pyplot as plt
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="pythondb"
)
mycursor = mydb.cursor()
mycursor.execute(
"SELECT cDay as 'Day', mOpen, mHigh, mLow, mClose, iVol FROM trday where cStockNo='SH600006' and cDay>'20140101' and cDay<'20140411' order by cDay") # //LIMIT 0,100
myresult = mycursor.fetchall()
df = pd.DataFrame(myresult)
df.columns = ["Day", "mOpen", "mHigh", "mLow", "mClose", "iVol"]
print(df)
plt.figure(figsize=(8, 4), dpi=80)
df['mClose'].plot(title="SH600006", color='b')
df['mClose'].rolling(10).mean().plot(color='r') #10 Days
#df['mClose'].rolling(5).mean().plot(color='r') #5 Days
rows = df.shape[0]
d = df['Day'][0:rows:10]
plt.xticks(range(0, rows, 10), d, rotation=45, fontsize=12)
plt.legend(['mClose', 'MA(10)'])
plt.show()