导入库¶
从tushare获取数据,使用holoviews绘图
In [1]:
import tushare as ts
In [3]:
%pylab inline
In [88]:
import numpy as np
import holoviews as hv
hv.extension('matplotlib')
单天数据查看¶
定义一个函数,可以查看某天某个数据项的分布图
In [25]:
def get_hist(date,attr):
df = ts.get_day_all(date)
start = df[attr].describe()['min']
stop = df[attr].describe()['max']
bin_array = np.linspace(start=start, stop=stop, num=50)
return df[attr].hist(bins=bin_array)
In [104]:
get_hist('2018-12-03','p_change')
Out[104]:
连续多天数据比较¶
In [192]:
df = ts.get_day_all('2018-12-03')
df1 = ts.get_day_all('2018-12-04')
df2 = ts.get_day_all('2018-12-05')
In [193]:
df = df.query('-10.01<p_change<10.01')
df1 = df1.query('-10.01<p_change<10.01')
df2 = df2.query('-10.01<p_change<10.01')
In [203]:
df['real_change'] = (df.price-df.open)/df.price*100
df = df.query('real_change<30')
df1['real_change'] = (df1.price-df1.open)/df1.price*100
df1 = df1.query('real_change<30')
df2['real_change'] = (df2.price-df2.open)/df2.price*100
df2 = df2.query('real_change<30')
In [204]:
points = hv.Points(df[['p_change','real_change']]).redim.range(x=(-5, 5), y=(-5, 5))
points1 = hv.Points(df1[['p_change','real_change']]).redim.range(x=(-5, 5), y=(-5, 5))
points2 = hv.Points(df2[['p_change','real_change']]).redim.range(x=(-5, 5), y=(-5, 5))
In [219]:
%%opts Histogram [height=750 ]
%%opts Histogram (alpha=0.3)
%%opts Points (alpha=0.6)
from holoviews.operation import histogram
xhist, yhist = (histogram(points, bin_range=(-5, 5), dimension=dim) *
histogram(points1, bin_range=(-5, 5), dimension=dim) *
histogram(points2, bin_range=(-5, 5), dimension=dim)
for dim in ['p_change','real_change'])
(points2 * points1 * points) << yhist(plot=dict(width=125)) << xhist(plot=dict(height=125))
Out[219]:
从右到左,黄色红色蓝色依次是周一周二周三的绝对涨幅和真实盘中涨幅,有没有发现数据变化规律??
In [220]:
points2 + points1 + points
Out[220]: