自选股票并绘制图像
1 ##自选股(代码、日期)合并保存 2 import tushare as ts 3 import pandas as pd 4 ##输入自选股 5 data_list=['600850','603722','002411','002444','002456','300011'] 6 StockPrices = pd.DataFrame() 7 for num_stock in data_list: 8 rescore=ts.get_hist_data(num_stock,start='2020-01-05',end='2020-02-26',ktype='D')##选择日期 9 rescore['stock_name']=num_stock 10 rescore=rescore.reset_index() 11 StockPrices=StockPrices.append(rescore) 12 StockPrices.to_excel(excel_writer='D:/stock_ana/all.xlsx',index=False) 13 Sp=pd.DataFrame(StockPrices,columns=(['stock_name','date','close'])) 14 pivoted=Sp.pivot('date','stock_name','close') 15 import matplotlib.pyplot as plt 16 fig=plt.figure() 17 plt.figure(figsize=(30,10)) 18 l=len(data_list) 19 color_list=['r','g','b','y','k','tan','c','olive','pink','purple'] 20 for i,m in zip(data_list,range(l)): 21 plt.plot(pivoted[i],'ko--',color=color_list[m]) 22 plt.tick_params(labelsize=13)##字体大小 23 plt.xticks(rotation=45)##旋转角度 24 plt.legend(loc='best')##标签位置 25 plt.savefig(r'D:/stock_ana/自选股分析.jpg')