Python 使用 Bokeh 和 pandas 绘制多条线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31520951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:08:31  来源:igfitidea点击:

Plotting multiple lines with Bokeh and pandas

pythonpandasbokeh

提问by bs123

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 行使用相同的颜色,见下文:

enter image description here

在此处输入图片说明

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.

提前致谢。

采纳答案by zpbruno

You need to provide a list of colors to multi_line. In your example, you would do, something like this:

您需要向 multi_line 提供颜色列表。在您的示例中,您会执行以下操作:

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:

产生:

multi_line plot

多线图

回答by Hugo Alain Oliva

Maintainers note: The bokeh.chartsAPI was deprecated and removed years ago

维护者注意:该bokeh.chartsAPI 多年前已被弃用和删除





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. Please see the following example located here:

您需要绘制时间序列图表。这将允许您轻松插入图例。TimeSeries 属性可以位于 bokeh._legacy_charts 下。请参阅位于此处的以下示例:

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