现在时间是:
当前位置:首 页 >> 数据可视>> 文章列表

Bokeh 折线图(1)

作者:   发布时间:2017-03-07 16:10:37   浏览次数:821

Bokeh基于D3,适于现代浏览器,示例已经满足本阶段的需求,果断舍弃Plotly(墙内太慢了!)

# coding: utf-8
# 代码示例:时间序列
import numpy as np from bokeh.layouts import gridplot from bokeh.plotting import figure, show, output_file from bokeh.sampledata.stocks import AAPL, GOOG, IBM, MSFT # 数据源,在Bokeh目录有

# 时间序列预处理函数
def datetime(x):
return np.array(x, dtype=np.datetime64) # 将np某一列作为时间序列,可以不用作为索引

p1 = figure(x_axis_type="datetime", title="Stock Closing Prices") # 置顶x轴的类型,和标题

p1.grid.grid_line_alpha=0.3 # 定义线条的alpha值,缺省1.0
p1.xaxis.axis_label = 'Date' # x轴线的标签
p1.yaxis.axis_label = 'Price' # y轴线的标签

# 直接定义线条及其属性,x轴数据,y轴数据,颜色,图例。
p1.line(datetime(AAPL['date']), AAPL['adj_close'], color='#A6CEE3', legend='AAPL')
p1.line(datetime(GOOG['date']), GOOG['adj_close'], color='#B2DF8A', legend='GOOG')
p1.line(datetime(IBM['date']), IBM['adj_close'], color='#33A02C', legend='IBM')
p1.line(datetime(MSFT['date']), MSFT['adj_close'], color='#FB9A99', legend='MSFT')
p1.legend.location = "top_left" # 图例标在左上

aapl = np.array(AAPL['adj_close'])
aapl_dates = np.array(AAPL['date'], dtype=np.datetime64)

window_size = 30 # 灵活的窗口
window = np.ones(window_size)/float(window_size)
aapl_avg = np.convolve(aapl, window, 'same') # np卷积

# 第二章图
p2 = figure(x_axis_type="datetime", title="AAPL One-Month Average")
p2.grid.grid_line_alpha = 0
p2.xaxis.axis_label = 'Date'
p2.yaxis.axis_label = 'Price'
p2.ygrid.band_fill_color = "olive" # 填充的颜色
p2.ygrid.band_fill_alpha = 0.1

p2.circle(aapl_dates, aapl, size=4, legend='close',
color='darkgrey', alpha=0.2) # 数据点用的圆圈

p2.line(aapl_dates, aapl_avg, legend='avg', color='navy') # 然后用折线
p2.legend.location = "top_left" # 左上

output_file("stocks.html", title="stocks.py example") # 输出路径

show(gridplot([[p1,p2]], plot_width=400, plot_height=400)) # 显示
# 400*400指的是像素






上一篇:没有了    下一篇:没有了

Copyright ©2018    易一网络科技|www.yeayee.com All Right Reserved.

技术支持:自助建站 | 领地网站建设 |短信接口 版权所有 © 2005-2018 lingw.net.粤ICP备16125321号 -5