Bokeh

如何使用Bokeh绘制时间序列数据

小樊
85
2024-05-20 10:55:34
栏目: 编程语言

要使用Bokeh绘制时间序列数据,首先需要导入必要的库和模块。然后创建一个Bokeh图形,设置图形的属性和样式,最后将时间序列数据传递给Bokeh图形进行绘制。

下面是一个示例代码,演示如何使用Bokeh绘制时间序列数据:

from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource
from bokeh.palettes import Category10
from bokeh.io import output_file

import pandas as pd

# 创建一个时间序列数据
data = pd.DataFrame({
    'date': pd.date_range('2022-01-01', periods=10),
    'value': [5, 7, 10, 15, 20, 25, 30, 35, 40, 45]
})

# 创建一个Bokeh图形
p = figure(x_axis_type='datetime', title='Time Series Data', plot_width=800, plot_height=400)

# 设置图形的属性和样式
p.line(x='date', y='value', source=ColumnDataSource(data), line_width=2, line_color=Category10[1][0])

# 保存图形到HTML文件
output_file('time_series_plot.html')

# 显示图形
show(p)

运行以上代码,将会生成一个包含时间序列数据的Bokeh图形,并保存为HTML文件。可以在浏览器中打开该HTML文件,查看绘制的时间序列数据图形。

0
看了该问题的人还看了