简体   繁体   English

使用 Bokeh 和 pandas 绘制多条线

[英]Plotting multiple lines with Bokeh and pandas

I would like to give a pandas dataframe to Bokeh to plot a line chart with multiple lines.我想给 Bokeh 提供一个 Pandas 数据框来绘制多条线的折线图。

The x-axis should be the df.index and each df.columns should be a separate line. x 轴应该是 df.index 并且每个 df.columns 应该是单独的一行。

This is what I would like to do:这就是我想做的:

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

toy_df = pd.DataFrame(data=np.random.rand(5,3), columns = ('a', 'b' ,'c'), index = pd.DatetimeIndex(start='01-01-2015',periods=5, freq='d'))   

p = figure(width=1200, height=900, x_axis_type="datetime") 
p.multi_line(df)
show(p)

However, I get the error:但是,我收到错误:

RuntimeError: Missing required glyph parameters: ys

Instead, I've managed to do this:相反,我设法做到了这一点:

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

toy_df = pd.DataFrame(data=np.random.rand(5,3), columns = ('a', 'b' ,'c'), index = pd.DatetimeIndex(start='01-01-2015',periods=5, freq='d'))   

ts_list_of_list = []
for i in range(0,len(toy_df.columns)):
    ts_list_of_list.append(toy_df.index)

vals_list_of_list = toy_df.values.T.tolist()

p = figure(width=1200, height=900, x_axis_type="datetime") 
p.multi_line(ts_list_of_list, vals_list_of_list)
show(p)

That (ineligantly) does the job but it uses the same color for all 3 lines, see below:这(不恰当地)完成了这项工作,但它对所有 3 行使用相同的颜色,见下文:

在此处输入图片说明

Questions:问题:

1) How can I pass a pandas dataframe to bokeh's multi_line? 1) 如何将 Pandas 数据帧传递给散景的 multi_line?

2) If not possible directly, how can I manipulate the dataframe data so that multi_line will create each line with a different color? 2)如果不能直接使用,如何操作数据帧数据,以便 multi_line 将创建具有不同颜色的每条线?

Thanks in advance.提前致谢。

You need to provide a list of colors to multi_line.您需要向 multi_line 提供颜色列表。 In your example, you would do, something like this:在您的示例中,您会执行以下操作:

p.multi_line(ts_list_of_list, vals_list_of_list, line_color=['red', 'green', 'blue'])

Here's a more general purpose modification of your second example that does more or less what you ended up with, but is a little more concise and perhaps more Pythonic:这是对您的第二个示例的更通用的修改,它或多或少地完成了您最终得到的内容,但更简洁一些,也许更像 Pythonic:

import pandas as pd
import numpy as np
from bokeh.palettes import Spectral11
from bokeh.plotting import figure, show, output_file
output_file('temp.html')

toy_df = pd.DataFrame(data=np.random.rand(5,3), columns = ('a', 'b' ,'c'), index = pd.DatetimeIndex(start='01-01-2015',periods=5, freq='d'))   

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

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

which yields:产生:

多线图

Maintainers note: The bokeh.charts API was deprecated and removed years ago维护者注意: bokeh.charts API 多年前已被弃用和删除



OBSOLETE:过时的:

You need to plot a Time Series chart.您需要绘制时间序列图表。 This will allow you to easily insert a legend.这将允许您轻松插入图例。 The TimeSeries attribute is could be located under bokeh._legacy_charts. TimeSeries 属性可以位于 bokeh._legacy_charts 下。 Please see the following example located here:请参阅位于此处的以下示例:

http://docs.bokeh.org/en/0.9.3/docs/user_guide/charts.html http://docs.bokeh.org/en/0.9.3/docs/user_guide/charts.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM