第一步、通过tushare模块爬取指定股票代码的数据存储到csv文件中。
知识点如下:
1.pip install 安装模块
2.tushare模块的 get_hist_data方法
3.df数据的.to_csv方法保存数据表
代码如下:
import tushare as ts
stock_info = ts.get_hist_data('600838')
stock_info.to_csv('600838.csv')
然后我们就会看到目录线面有一个.csv文件存放了600838这只股票的全部历史数据了
第二步、通过pyecharts的Line方法绘制股指走势图
知识点如下:
1.from pyecharts.charts impot +图表类型 导入需要的图表类型
2.from pyecharts import options as ops 使用pyecharts的配置项
3.使用.add_xaxis/add_yaxis等方法向图表对象中写入绘制数据(内存中绘图)
4.使用to_list()方法把df数据转换为list对象
5.set_global_opts来配置图片参数(这块有点难度,需要加深理解)
6.rend()方法进行图形的渲染操作
7.df.sort_index对df数据进行排序
import tushare as ts
import pandas as pd
from pyecharts.charts import Line
from pyecharts import options as opts
df = pd.read_csv('600838.csv',index_col = 0)
df.sort_index(inplace=True)
line = Line()
line.add_xaxis(df.index.to_list())
line.add_yaxis('Open',df.open.round(2).tolist())
line.add_yaxis('Close',df.close.round(2).to_list())
line.set_global_opts(
title_opts = opts.TitleOpts(title='600838'),
tooltip_opts = opts.TooltipOpts(trigger = 'axis',axis_pointer_type = 'cross')
)
line.render()
最后我们生成了html文件,可以在浏览器中打开,注意这是一个交互式的图表,可以点击上面的按钮选择需要显示的图像。