gpt4 book ai didi

python - 如何从 csv 绘制 Bokeh 多行数据框

转载 作者:行者123 更新时间:2023-12-01 01:04:19 24 4
gpt4 key购买 nike

我有一个从 .csv 读取的数据框,格式如下:

version, 2x8x8, 2x8x10, 2x8x12, ...
v1.0.0, 10.2, 9.2, 8.2,
v1.0.1, 11.3, 10.4, 10.2,
v1.0.2, 9.5, 9.3, 9.1,
...

我想将此数据框绘制为 Bokeh 中的多线图,其中每一列都是其自己的线。 x 轴是版本号,y 轴是除标题之外的列内容。

我尝试引用 bokeh docs但我找不到像 bokeh 所期望的那样将列提取为“列表的列表”的最佳方法。

# produces empty plot
f = figure(title='Example title')
versions = list(df['version'])
values = [list(df['2x8x8']), list(df['2x8x10']), ...]
f.multi_line(xs=versions, ys=values)

当我尝试使用备用 ColumnDataSource 方法(也在 bokeh docs 中指定)时,该图采用我所有的 y 值并为每个值创建一条新线。

# produces plot seen below
df = pd.read_csv(my.csv)
data_source = ColumnDataSource(df)
f = figure(title="Example")
f.line(x='version', y='2x8x8', source=data_source, line_width=2, legend='2x8x8')
f.line(x='version', y='2x8x10', source=data_source, line_width=2, legend='2x8x10')
f.xaxis.axis_label = 'version'

非常感谢任何帮助!

enter image description here

最佳答案

我认为这就是你想要的(在 Bokeh v1.0.4 上测试):

import pandas as pd
import numpy as np
from bokeh.palettes import Spectral11
from bokeh.plotting import figure, show

toy_df = pd.DataFrame(data = {'version': ['v1.0.0', 'v1.0.1', 'v1.0.2', 'v1.0.3'],
'2x8x8': [10.2, 11.3, 9.5, 10.9],
'2x8x10': [9.2, 10.4, 9.3, 9.9],
'2x8x12': [8.2, 10.2, 9.1, 11.1]}, columns = ('version', '2x8x8' , '2x8x10', '2x8x12'))

numlines = len(toy_df.columns)
mypalette = Spectral11[0:numlines]

p = figure(width = 500, height = 300, x_range = toy_df['version'])
p.multi_line(xs = [toy_df['version'].values] * numlines,
ys = [toy_df[name].values for name in toy_df],
line_color = mypalette,
line_width = 5)
show(p)

结果:

enter image description here

关于python - 如何从 csv 绘制 Bokeh 多行数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55524084/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com