如何使用Bokeh的ColumnDataSource实现从CSV或Excel文件读取数据

1429
2024/5/15 11:24:22
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

要使用Bokeh的ColumnDataSource从CSV或Excel文件中读取数据,可以按照以下步骤操作:

  1. 导入必要的库:
from bokeh.io import output_file, show
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource
import pandas as pd
  1. 读取CSV或Excel文件中的数据:
df = pd.read_csv('data.csv') # 从CSV文件中读取数据
# df = pd.read_excel('data.xlsx') # 从Excel文件中读取数据
  1. 将数据转换为ColumnDataSource对象:
source = ColumnDataSource(df)
  1. 创建绘图对象并绘制图表:
p = figure(title='Data from CSV or Excel', x_axis_label='X', y_axis_label='Y')
p.circle('x', 'y', source=source, size=8, color='blue')
  1. 显示绘图:
output_file('data_plot.html')
show(p)

这样就可以使用Bokeh的ColumnDataSource实现从CSV或Excel文件读取数据并绘制图表了。

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: 如何将Bokeh图表与Jupyter Notebook无缝集成