I am working on some Stock (Time-Series) data. I am using Bokeh to visualize the said data.
I was trying to visualize Closing stock Price (stored in a column named Close
). Now there's another column Bool
which has boolean data (0 or 1) based on certain calculations I did on the data.
I have to plot the Closing stock such that the colour in a line plot that alters colours between two values of 1 in the Bool
column. Here is a real example of some values of the Bool
column.
0
1
0
0
1
1
0
1
1
0
0
1
So the Close
plot will be blue except for the 0's between two 1's in the Bool
column. For those cases, the plot has to be red.
I am not very used to using Bokeh so it would be great if you can help me out here
Did you mean something like this? (works for Bokeh v1.0.4)
import pandas as pd
import numpy as np
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, show
bools = [0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1]
colors = ['red' if bool == 0 else 'blue' for bool in bools]
df = pd.DataFrame(data = np.random.rand(len(bools), 1), columns = ['close_price'], index = pd.date_range(start = '01-01-2015', periods = len(bools), freq = 'd'))
xs, ys = [], []
for xs_value, ys_value in zip(df.index.values, df['close_price'].values):
if len(xs) > 0 and len(ys) > 0:
xs.append([xs[-1][1], xs_value])
ys.append([ys[-1][1], ys_value])
else:
xs.append([xs_value, xs_value])
ys.append([ys_value, ys_value])
source = ColumnDataSource(dict(xs = xs, ys = ys, color = colors))
p = figure(width = 500, height = 300, x_axis_type = "datetime")
p.multi_line(xs = 'xs',
ys = 'ys',
color = 'color',
source = source,
line_width = 3)
show(p)
Result:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.