[英]How to plot bokeh multiline dataframe from csv
I have a data frame read in from a .csv with the following format: 我有一个从.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,
...
I want to plot this data frame as a multiline plot in bokeh where each column is its own line. 我想将此数据框绘制为bokeh中的多线图,其中每一列都是自己的线。 The x axis being version num, and the y values being the content of the column excluding the header. x轴是版本号,y值是不包括标题的列的内容。
I have tried referencing the bokeh docs themselves but I cannot find the best way to extract the columns as "lists of lists" as bokeh expects. 我曾尝试参考bokeh文档本身,但我找不到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)
When I attempt to use the alternate ColumnDataSource
approach, also specified in the bokeh docs , the plot takes all my y values and makes a new line for each. 当我尝试使用在bokeh docs中也指定的替代ColumnDataSource
方法时,该图将获取我的所有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'
Any help is greatly appreciated! 任何帮助是极大的赞赏!
I think this is what you want (tested on Bokeh v1.0.4): 我认为这就是您想要的(在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)
Result: 结果:
Another version including labels. 包括标签的另一个版本。 This is a different approach using explicitly ColumnDataSource
instead of pandas DataFrame
. 这是使用显式ColumnDataSource
而不是pandas DataFrame
的另一种方法。
Please note that if you want to use p.legend.click_policy = "hide"
to toggle visibility or mute separate lines then you should rather use line
glyph instead of multi_line
. 请注意,如果要使用p.legend.click_policy = "hide"
来切换可见性或使单独的行静音,则应该使用line
字形而不是multi_line
。 This code works for Bokeh v1.0.4 该代码适用于Bokeh v1.0.4
from bokeh.palettes import Spectral11
from bokeh.plotting import figure, show
from bokeh.models import Legend, ColumnDataSource
versions = ['v1.0.0', 'v1.0.1', 'v1.0.2', 'v1.0.3']
data = {'version': [versions] * 3,
'values': [[10.2, 11.3, 9.5, 10.9],
[9.2, 10.4, 9.3, 9.9],
[8.2, 10.2, 9.1, 11.1]],
'columns': ['2x8x8', '2x8x10', '2x8x12'],
'color': Spectral11[0:3] }
source = ColumnDataSource(data)
p = figure(width = 500, height = 300, x_range = versions)
p.multi_line(xs = 'version',
ys = 'values',
color = 'color',
legend = 'columns',
line_width = 5,
source = source)
show(p)
Result: 结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.