-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbokeh_example.py
213 lines (158 loc) · 6.72 KB
/
bokeh_example.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
'''Bokeh - Python Interactive Data Visualization Library'''
# Bokeh is a Python interactive visualization library that targets modern web
# browsers for presentation. Its goal is to provide elegant, concise
# construction of graphics in the style of D3.js, and to extend this capability
# with high-performance interactivity over very large or streaming datasets.
# Bokeh is good for creating interactive plots, dashboards & data applications.
# pip install bokeh
# In examples below, we'll also use pandas:
# pip install pandas, openpyxl
# https://bokeh.pydata.org/en/latest/
# https://bokeh.pydata.org/en/latest/docs/user_guide/styling.html
# Bokeh has 2 different interfaces for generating models.
# – bokeh.models: used by application developers, it's a low-level interface
# – bokeh.plotting: a medium-level interface with room for customizing
# bk.plotting
# -----------------------------------------------------------------------------
from bokeh.plotting import figure, output_file, show
# https://bokeh.pydata.org/en/latest/docs/reference/colors.html#bokeh-colors-named
c = ['deepskyblue', 'olive', 'darkred', 'goldenrod', 'skyblue', 'orange', 'salmon']
# create a figure object
p = figure(plot_width=600, plot_height=400, title='Practice Plot')
# figure attributes can also be added like this:
p.title.text_color = c[6]
p.title.text_font = 'Helvetica'
p.title.text_font_size = '16pt'
p.title.text_font_style = 'normal'
p.yaxis.minor_tick_line_color = c[4]
p.xaxis.minor_tick_line_color = None
p.yaxis.axis_label = 'y axis label'
p.xaxis.axis_label = 'x axis label'
p.axis.axis_label_text_font_style = 'normal'
p.axis.axis_label_text_color = c[1]
# add 'glyphs' to the figure object
p.circle([1, 2, 3, 4, 5], [10, 3, 8, 4, 7], size=12, color=c[6], alpha=0.5)
# Note, you're not limited to circles, you can also use other markers such as:
# – asterisk()
# – circle()
# – circle_cross()
# – circle_x()
# – cross()
# – diamond()
# – diamond_cross()
# – inverted_triangle()
# – square()
# – square_cross()
# – square_x()
# – triangle()
# – x()
# you can also pass a list of sizes to be applied to each plot
p.x([1, 2, 3], [5, 6, 2], size=[10, 15, 20], color=c[5], alpha=0.7)
# define an output file
output_file('demos/scatter_plot1.html')
# show the plot
show(p)
# regarding output_file('file.html')
# -----------------------------------------------------------------------------
# Note that there is a mode parameter that you can pass to the output_file
# function that will determine whether the html creates links to the
# necessary javascript and css or whether these files are created and saved to
# your working directory. The default mode is 'cdn' which stands for
# 'content delivery network'. It links to css and javascript files on a remote
# server. The options are as follows:
# – cdn
# – relative
# – absolute
# – inline
# output_file('scatter_plot2.html', mode='relative')
# Both relative and absolute will link to files located in the bokeh package
# directory. For me this is:
# Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/
# site-packages/bokeh/server/static/css/bokeh.min.css
# the inline option will dump all the css and js into the html doc (ew!).
# bk.plotting with pandas, and more bokeh style examples
# -----------------------------------------------------------------------------
from bokeh.plotting import figure, output_file, show
import pandas
df = pandas.read_excel('data/verlegenhuken.xlsx', sheet_name=0)
print(df.columns)
# Index(['Year', 'Month', 'Day', 'Hour', 'Temperature', 'Pressure'],...)
temperatures = df.Temperature / 10 # <class 'pandas.core.series.Series'>
pressure = df.Pressure / 10
toools = "pan, wheel_zoom, box_zoom, reset"
p2 = figure(sizing_mode='stretch_both', tools=toools)
p2.title.text = 'Temperature and Air Pressure - Verlegenhuken'
p2.yaxis.axis_label = 'Pressure (hPa)'
p2.xaxis.axis_label = 'Temperature (ºC)'
p2.toolbar_location = 'right' # below, above, left, right, None
p2.circle(temperatures, pressure, size=3, color=c[0], alpha=0.2)
output_file('demos/scatter_plot2.html')
show(p2)
# Time series plots
# -----------------------------------------------------------------------------
# The data for this plot comes from:
# https://www.kaggle.com/sudalairajkumar/cryptocurrencypricehistory
# https://www.cryptocompare.com/coins/neo/charts/BTC?p=ALL
from bokeh.plotting import figure, output_file, show
import pandas
df = pandas.read_csv('data/neo_btc.csv', parse_dates=['timeDate'])
dates = df.timeDate
close = df.close
p3 = figure(x_axis_type='datetime', sizing_mode='stretch_both')
p3.title.text = 'Neo Coin - Historical data'
p3.yaxis.axis_label = 'Close Price (BTC)'
# p3.xaxis.axis_label = 'Date'
p3.title.text_color = 'black'
p3.title.text_font = 'Helvetica'
p3.title.text_font_size = '16pt'
p3.title.text_font_style = 'normal'
p3.axis.axis_label_text_font_style = 'bold'
p3.axis.axis_label_text_font_size = '10pt'
p3.axis.axis_label_text_color = 'black'
p3.toolbar_location = None
p3.ygrid.minor_grid_line_color = 'navy'
p3.ygrid.minor_grid_line_alpha = 0.05
p3.xgrid.grid_line_color = None
p3.xaxis.minor_tick_line_color = 'black'
p3.xaxis[0].ticker.desired_num_ticks = 10
p3.min_border_bottom = 100
p3.min_border_top = 50
p3.line(dates, close, line_width=2, color=c[5], alpha=0.9)
output_file('demos/line_plot1.html')
show(p3)
# Hover tools
# -----------------------------------------------------------------------------
# The data for this plot comes from:
# https://www.kaggle.com/sudalairajkumar/cryptocurrencypricehistory
# https://www.cryptocompare.com/coins/neo/charts/BTC?p=ALL
from bokeh.plotting import figure, output_file, show
from bokeh.models import HoverTool, ColumnDataSource
import pandas
df = pandas.read_csv('data/neo_usd.csv', parse_dates=['Date'])
dates = df['Date']
close = df['Close']
p4 = figure(x_axis_type='datetime', sizing_mode='stretch_both')
p4.title.text = 'Neo Coin - Historical data'
p4.yaxis.axis_label = 'Close Price (USD)'
p4.title.text_color = 'black'
p4.title.text_font = 'Helvetica'
p4.title.text_font_size = '16pt'
p4.title.text_font_style = 'normal'
p4.axis.axis_label_text_font_style = 'bold'
p4.axis.axis_label_text_font_size = '10pt'
p4.axis.axis_label_text_color = 'black'
p4.toolbar_location = None
p4.ygrid.minor_grid_line_color = 'navy'
p4.ygrid.minor_grid_line_alpha = 0.05
p4.xgrid.grid_line_color = None
p4.xaxis.minor_tick_line_color = 'black'
p4.xaxis[0].ticker.desired_num_ticks = 10
p4.min_border_bottom = 100
p4.min_border_top = 50
cds = ColumnDataSource(df)
df['Date_str'] = df['Date'].dt.strftime('%m-%d-%Y')
hover = HoverTool(tooltips=[('Date', '@Date_str'), ('Close price', '@Close')])
p4.add_tools(hover)
p4.line(x="Date", y="Close", line_width=2, color=c[4], alpha=0.9, source=cds)
output_file('demos/line_plot2.html')
show(p4)