来自熊猫df的python bokeh ohlc情节

时间:2017-03-31 21:51:14

标签: python plot bokeh candlestick-chart

运行此代码后,我没有收到任何错误,没有绘图。

print type(_df)
print _df
<class 'pandas.core.frame.DataFrame'>
          datetime    open    high     low   close
0  2016-12-23 12:35:00  761.05  761.05  760.90  760.90
1  2016-12-23 12:40:00  761.05  761.05  760.61  760.88
2  2016-12-23 12:45:00  760.88  761.05  760.80  761.00
3  2016-12-23 12:50:00  760.91  761.00  760.36  760.48
4  2016-12-23 12:55:00  760.42  760.66  760.03  760.20
5  2016-12-23 13:00:00  760.05  760.97  760.05  760.58
6  2016-12-23 13:05:00  760.60  760.94  760.57  760.73
7  2016-12-23 13:10:00  760.77  760.92  760.57  760.66
8  2016-12-23 13:15:00  760.62  761.00  760.56  760.91
9  2016-12-23 13:20:00  760.88  761.06  760.55  760.55
10 2016-12-23 13:25:00  760.68  760.88  760.50  760.68
11 2016-12-23 13:30:00  760.78  760.82  760.55  760.55
12 2016-12-23 13:35:00  760.57  760.82  760.52  760.76
13 2016-12-23 13:40:00  760.75  761.09  760.75  760.95
14 2016-12-23 13:45:00  760.89  761.74  760.86  761.13

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~

from math import pi
from bokeh.plotting import figure, show, output_file

inc = _df.close > _df.open
dec = _df.open > _df.close
w = 12*60*60*1000/2000

TOOLS = "pan,wheel_zoom,box_zoom,reset,save"

p = figure(x_axis_type="datetime", tools=TOOLS, plot_width=1000, title = "Candlestick")
p.xaxis.major_label_orientation = pi/4
p.grid.grid_line_alpha=0.3

p.segment(_df.datetime, _df.high, _df.datetime, _df.low, color="black")
p.vbar(_df.datetime[inc], w, _df.open[inc], _df.close[inc], fill_color="#D5E1DD", line_color="black")
p.vbar(_df.datetime[dec], w, _df.open[dec], _df.close[dec], fill_color="#F2583E", line_color="black")

output_file("candlestick.html", title="candlestick.py example")
show(p)

同时,此代码有效:

import io
from math import pi
import pandas as pd
from bokeh.plotting import figure, show, output_file

df = pd.read_csv(
    io.BytesIO(
        b'''datetime,open,high,low,close
2016-12-23 12:35:00,  761.05,  761.05,  760.90,  760.90
2016-12-23 12:40:00,  761.05,  761.05,  760.61,  760.88
2016-12-23 12:45:00,  760.88,  761.05,  760.80,  761.00
2016-12-23 12:50:00,  760.91,  761.00,  760.36,  760.48
2016-12-23 12:55:00,  760.42,  760.66,  760.03,  760.20
2016-12-23 13:00:00,  760.05,  760.97,  760.05,  760.58
2016-12-23 13:05:00,  760.60,  760.94,  760.57,  760.73
2016-12-23 13:10:00,  760.77,  760.92,  760.57,  760.66
2016-12-23 13:15:00,  760.62,  761.00,  760.56,  760.91
2016-12-23 13:20:00,  760.88,  761.06,  760.55,  760.55
2016-12-23 13:25:00,  760.68,  760.88,  760.50,  760.68
2016-12-23 13:30:00,  760.78,  760.82,  760.55,  760.55
2016-12-23 13:35:00,  760.57,  760.82,  760.52,  760.76
2016-12-23 13:40:00,  760.75,  761.09,  760.75,  760.95
2016-12-23 13:45:00,  760.89,  761.74,  760.86,  761.13'''
    )
)

#print df.tail()
df["datetime"] = pd.to_datetime(df["datetime"])
print df
inc = df.close > df.open
dec = df.open > df.close
w = 12*60*60*1000/2000 #

TOOLS = "pan,wheel_zoom,box_zoom,reset,save"

p = figure(x_axis_type="datetime", tools=TOOLS, plot_width=1000, title = "AMZN Candlestick")
p.xaxis.major_label_orientation = pi/4
p.grid.grid_line_alpha=0.3

p.segment(df.datetime, df.high, df.datetime, df.low, color="black")
p.vbar(df.datetime[inc], w, df.open[inc], df.close[inc], fill_color="#D5E1DD", line_color="black")
p.vbar(df.datetime[dec], w, df.open[dec], df.close[dec], fill_color="#F2583E", line_color="black")

output_file("candlestick.html", title="candlestick.py example")

show(p)  # open a browser

这里的print df语句产生完全相同的数据帧

              datetime    open    high     low   close
0  2016-12-23 12:35:00  761.05  761.05  760.90  760.90
1  2016-12-23 12:40:00  761.05  761.05  760.61  760.88
2  2016-12-23 12:45:00  760.88  761.05  760.80  761.00
3  2016-12-23 12:50:00  760.91  761.00  760.36  760.48
4  2016-12-23 12:55:00  760.42  760.66  760.03  760.20
5  2016-12-23 13:00:00  760.05  760.97  760.05  760.58
6  2016-12-23 13:05:00  760.60  760.94  760.57  760.73
7  2016-12-23 13:10:00  760.77  760.92  760.57  760.66
8  2016-12-23 13:15:00  760.62  761.00  760.56  760.91
9  2016-12-23 13:20:00  760.88  761.06  760.55  760.55
10 2016-12-23 13:25:00  760.68  760.88  760.50  760.68
11 2016-12-23 13:30:00  760.78  760.82  760.55  760.55
12 2016-12-23 13:35:00  760.57  760.82  760.52  760.76
13 2016-12-23 13:40:00  760.75  761.09  760.75  760.95
14 2016-12-23 13:45:00  760.89  761.74  760.86  761.13

换句话说,我将相同的数据传递给散景,其中只有一个产生了一个情节,任何人都知道问题可能是什么?

1 个答案:

答案 0 :(得分:0)

尝试将df["datetime"] = pd.to_datetime(df["datetime"])替换为df.datetime = pd.to_datetime(df.datetime)

相关问题