forked from FrankBGao/HeatMap_for_TuShare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
heatmap_real_time.py
114 lines (102 loc) · 3.68 KB
/
heatmap_real_time.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# -*- coding: utf8 -*-
from bokeh.models import ColumnDataSource, HoverTool
from bokeh.plotting import figure
from bokeh.embed import components
from datetime import datetime
def get_index(data):
data['index_len'] = range(len(data))
return data
def get_heatmap(day,one_day):
one_day = one_day.drop_duplicates()
print len(one_day)
# I don't want 创业板
one_day['code_is'] = one_day['code'].apply(lambda x: x[0])
one_day = one_day[one_day['code_is'] != '3']
one_day = one_day.groupby('c_name', as_index=False).apply(get_index)
romans = one_day['c_name'].drop_duplicates().values # [str(x) for x in range(0, one_day['index_len'].max())]
group_range = [str(x) for x in range(0, one_day['index_len'].max())]
colormap = {
-11: '#005824',
-10: '#005824',
-9: '#1A693B',
-8: '#347B53',
-7: '#4F8D6B',
-6: '#699F83',
-5: '#83B09B',
-4: '#9EC2B3',
-3: '#B8D4CB',
-2: '#D2E6E3',
-1: '#EDF8FB',
0: '#ededed',
1: '#ffcfdc',
2: '#ffcccc',
3: '#ff8080',
4: '#ff5959',
5: '#ff4040',
6: '#d90000',
7: '#b20000',
8: '#7f0000',
9: '#660000',
10: '#400000',
11: '#400000'
}
if 'trade' in one_day.columns:
source = ColumnDataSource(
data=dict(
group=[unicode(x) for x in one_day["c_name"]],
period=[unicode(y) for y in one_day["index_len"]],
type_color=[colormap[x] for x in one_day["rate"]],
code=one_day["code"],
amount=one_day["amount"],
trade=one_day["trade"],
c_name=one_day["c_name"],
name=one_day["name"],
rate=one_day["rate"],
volume=one_day["volume"],
high=one_day["high"],
low=one_day["low"],
open=one_day["open"],
)
)
p = figure(title=u"今日大盘行情"+ str(datetime.now()) , tools="pan,wheel_zoom,box_zoom,reset,save,hover",
x_range=group_range, y_range=list(reversed(romans)))
else:
source = ColumnDataSource(
data=dict(
group=[unicode(x) for x in one_day["c_name"]],
period=[unicode(y) for y in one_day["index_len"]],
type_color=[colormap[x] for x in one_day["rate"]],
code=one_day["code"],
amount=one_day["amount"],
close=one_day["close"],
c_name=one_day["c_name"],
name=one_day["name"],
rate=one_day["rate"],
volume=one_day["volume"],
high=one_day["high"],
low=one_day["low"],
open=one_day["open"],
)
)
p = figure(title=u"大盘行情" + day, tools="pan,wheel_zoom,box_zoom,reset,save,hover",
x_range=group_range, y_range=list(reversed(romans)))
p.plot_width = 1200
p.toolbar_location = None
p.outline_line_color = None
p.rect("period", "group", 0.9, 0.9, source=source,
fill_alpha=0.6, color="type_color")
p.select_one(HoverTool).tooltips = [
("c_name", "@c_name"),
("name", "@name"),
("code", "@code"),
("rate", "@rate"),
("volume", "@volume"),
("amount", "@amount"),
("high", "@high"),
("low", "@low"),
("open", "@open"),
("close", "@close"),
("trade", "@trade"),
]
script, div = components(p)
return div + script