candlestick2_ochl 画k线图

取得股票日线

def saveStockByTS(code):
    start = '2020-01-01'
    end = '2020-12-31'
 
    # ts.get_hist_data(code=code,start=start,end=end).to_csv('d:\\ch5\\'+code+'.csv',columns=['open','high','close','low','volume'])
    df = ts.get_hist_data(code=code,start=start,end=end)
    df.sort_index().to_csv('d:\\ch5\\'+code+'.csv',columns=['open','high','close','low','volume'])
     
code= "600895"
saveStockByTS(code)

画K线图

import pandas as pd 
import matplotlib.pyplot as plt 
from mpl_finance import  candlestick2_ochl
df = pd.read_csv('d:/ch5/600895.csv',encoding='gbk',index_col=0)
print(df)
fig = plt.figure()
ax = fig.add_subplot(111)
candlestick2_ochl(ax=ax,opens=df['open'].values,closes=df['close'].values,highs=df['high'].values,lows=df['low'].values,width=0.75,colorup='red',colordown='green')
plt.xticks(range(len(df.index.values)),df.index.values,rotation=30)
ax.grid(True)
plt.title('k-line')
plt.show()

 

你可能感兴趣的:(python,python)