Skip to content

Commit 0c5ac20

Browse files
committed
correct homework
1 parent 36a1d04 commit 0c5ac20

File tree

1 file changed

+117
-1
lines changed

1 file changed

+117
-1
lines changed

D23 BOKEH/homework.py

+117-1
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,127 @@
88
# 讓網頁直接輸出在NOTEBOOK
99
output_notebook()
1010

11+
#Bokeh官方有提供sample_data給大家練習,gallery豐富的範例都取自sample_data。
12+
#下載sample_data指令為bokeh.sampledata.download(),直接貼在jupyter執行。檔案會下載到bokeh module裡。
13+
import bokeh.sampledata
14+
bokeh.sampledata.download()
15+
16+
1117
# 基本條形圖 條形圖是常見且重要的繪圖類型。通過 Bokeh,可以輕鬆創建各種堆疊或嵌套的條形圖,並通常處理分類數據。 請使用 vbar 方法創建的,用於繪製垂直條。
1218
# 1. 建立簡單的水果資料集
1319
# fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
1420
# counts = [5, 3, 4, 2, 4, 6]
21+
from bokeh.models import ColumnDataSource
22+
from bokeh.palettes import Spectral6
23+
from bokeh.models import ColumnDataSource, HoverTool
24+
from bokeh.plotting import figure
25+
26+
fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
27+
counts = [5, 3, 4, 2, 4, 6]
28+
29+
source = ColumnDataSource(data=dict(fruits=fruits, counts=counts, color=Spectral6))
30+
31+
p = figure(x_range=fruits, plot_height=250, y_range=(0, 9), title="Fruit Counts")
32+
p.vbar(x='fruits', top='counts', width=0.9, color='color', legend_field="fruits", source=source)
33+
34+
p.xgrid.grid_line_color = None
35+
p.legend.orientation = "horizontal"
36+
p.legend.location = "top_center"
37+
38+
show(p)
1539
# 2. 利用 Source 建立字典,再用 figure 輸出 BAR 圖
1640
# source = ColumnDataSource(data=dict(fruits=fruits, counts=counts, color=Spectral6))
41+
from bokeh.models import FactorRange
42+
from bokeh.transform import factor_cmap
43+
44+
fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
45+
years = ['2015', '2016', '2017']
46+
47+
data = {'fruits' : fruits,
48+
'2015' : [2, 1, 4, 3, 2, 4],
49+
'2016' : [5, 3, 3, 2, 4, 6],
50+
'2017' : [3, 2, 4, 4, 5, 3]}
51+
52+
# this creates [ ("Apples", "2015"), ("Apples", "2016"), ("Apples", "2017"), ("Pears", "2015), ... ]
53+
x = [ (fruit, year) for fruit in fruits for year in years ]
54+
counts = sum(zip(data['2015'], data['2016'], data['2017']), ()) # like an hstack
55+
56+
source = ColumnDataSource(data=dict(x=x, counts=counts))
57+
58+
p = figure(x_range=FactorRange(*x), plot_height=250, title="Fruit Counts by Year")
59+
60+
#p.vbar(x='x', top='counts', width=0.9, source=source)
61+
62+
p.vbar(x='x', top='counts', width=0.9, source=source, line_color="white",
63+
64+
# use the palette to colormap based on the the x[1:2] values
65+
fill_color=factor_cmap('x', palette=['firebrick', 'olive', 'navy'], factors=years, start=1, end=2))
66+
67+
p.y_range.start = 0
68+
p.x_range.range_padding = 0.1
69+
p.xaxis.major_label_orientation = 1
70+
p.xgrid.grid_line_color = None
71+
72+
show(p)
1773
# 3. Bokeh 官方有提供 sample_data 給大家練習,gallery 豐富的範例都取自 sample_data,對比官方的資料格式就能輕鬆模仿應用,下載 股市資料
18-
# 4. 使用 HoverTool(游標滑過時顯示資料);Click_policy (藉由標籤控制數值顯示)
74+
75+
# 4. 使用 HoverTool(游標滑過時顯示資料);Click_policy (藉由標籤控制數值顯示)
76+
import bokeh.io
77+
from bokeh.resources import INLINE
78+
from bokeh.models import HoverTool
79+
from bokeh.palettes import Spectral4
80+
from bokeh.plotting import figure, output_file, show, output_notebook, ColumnDataSource
81+
from bokeh.sampledata.stocks import AAPL, GOOG, IBM, MSFT
82+
import pandas as pd
83+
84+
# 環境 settings
85+
bokeh.io.reset_output()
86+
bokeh.io.output_notebook(INLINE)
87+
88+
89+
# set hover
90+
## HoverTool
91+
# 游標滑過時顯示資料,date格式需要轉換,不然會是timestamp
92+
hover = HoverTool(
93+
tooltips = [
94+
("date", "@date"),
95+
("close", "@open"),
96+
("close", "@close"),
97+
("high", "@high"),
98+
("low", "@low"),
99+
("volume","@volume")
100+
],
101+
formatters={"@date":"datetime"}
102+
)
103+
104+
# set figure
105+
p = figure(
106+
plot_width=1000,
107+
plot_height=400,
108+
x_axis_type="datetime",
109+
tools=[hover,"pan,box_zoom,reset,save"],
110+
)
111+
p.title.text = 'Stock_Price--Click on legend entries to mute the corresponding lines and show daily details in hover'
112+
113+
# use ColumnDataSource to control
114+
# click_policy
115+
# 藉由標籤控制數值顯示
116+
# hide為隱藏,mute為切換自訂顯示模式
117+
# 可在muted_color控制顏色, muted_alpha控制顏色濃淡
118+
119+
for data, name, color in zip([AAPL, IBM, MSFT, GOOG], ["AAPL", "IBM", "MSFT", "GOOG"], Spectral4):
120+
df = pd.DataFrame(data)
121+
df['date'] = pd.to_datetime(df['date'])
122+
source = ColumnDataSource(df)
123+
p.line(x="date",y="close", line_width=2, color=color, alpha=0.8,
124+
muted_color=color, muted_alpha=0.2, legend_label=name,source=source)
125+
126+
127+
p.legend.location = "top_left"
128+
# use hide or mute
129+
p.legend.click_policy="mute"
130+
131+
# output_file("interactive_legend.html", title="interactive_legend.py example")
132+
133+
show(p)
134+
output_notebook()

0 commit comments

Comments
 (0)