Python 如何使用Bokeh使用HoverTool创建多线图?

Python 如何使用Bokeh使用HoverTool创建多线图?,python,bokeh,Python,Bokeh,从一个像下面这样的熊猫数据框中,我只是尝试创建一个多线绘图加鼠标悬停工具;然而,我找不到任何与我的具体案例类似的例子。以下是我的示例代码: import pandas as pd import numpy as np # Dataframe (just toy data, this will be an import of a much larger dataset) index = ['2018-01-31', '2018-02-28', '2018-03-31', '2018-04-30'

从一个像下面这样的熊猫数据框中,我只是尝试创建一个多线绘图加鼠标悬停工具;然而,我找不到任何与我的具体案例类似的例子。以下是我的示例代码:

import pandas as pd
import numpy as np

# Dataframe (just toy data, this will be an import of a much larger dataset)
index = ['2018-01-31', '2018-02-28', '2018-03-31', '2018-04-30', '2018-05-31', 
         '2018-06-30', '2018-07-31', '2018-08-31', '2018-09-30', '2018-10-31']

columns = ['Argentina', 'Turkey', 'Mexico']

np.random.seed(123)
data = np.random.rand(10, 3)

df = pd.DataFrame(index=index, columns=columns, data=data)
df.index = pd.to_datetime(df.index)
df.index.name = 'Date'

# Attempt at plot (obviously doesn't work)
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource, HoverTool
from bokeh.io import output_notebook, show
output_notebook()

source = ColumnDataSource(df)

p = figure(plot_height=400)
p.multi_line(xs='Date', ys=columns, source=source)

p.add_tools(HoverTool(tooltips=[('Country', '@???'),
                                ('Date', '@Date'),
                                ('Value', '@???')]))

show(p)
xs和ys应该是一个列表列表,这就是它不起作用的原因。 我将多行替换为3条普通行,现在应该可以正常工作了。 如果您确实想使用多行,则应按如下方式格式化数据:

index = ['2018-01-31', '2018-02-28', '2018-03-31', '2018-04-30', '2018-05-31', 
         '2018-06-30', '2018-07-31', '2018-08-31', '2018-09-30', '2018-10-31']
source = ColumnDataSource(dict(
"xs": [index, index, index],
"ys": np.random.rand(3, 10).tolist()
)) 
使用行而不是多行处理代码:

from bokeh.plotting import figure
from bokeh.models import ColumnDataSource, HoverTool
from bokeh.io import output_notebook, show
import pandas as pd
import numpy as np

index = ['2018-01-31', '2018-02-28', '2018-03-31', '2018-04-30', '2018-05-31', 
         '2018-06-30', '2018-07-31', '2018-08-31', '2018-09-30', '2018-10-31']

columns = ['Argentina', 'Turkey', 'Mexico']

np.random.seed(123)
data = np.random.rand(10, 3)

df = pd.DataFrame(index=index, columns=columns, data=data)
df['Date'] = index

output_notebook() 
source = ColumnDataSource(df)

p = figure(plot_height=400, x_range=index)
p.line(x='Date', y='Argentina', source=source, color='red', legend='Argentina ', name='Argentina')
p.line(x='Date', y='Turkey', source=source, color='blue', legend='Turkey ', name='Turkey')
p.line(x='Date', y='Mexico', source=source, color='green', legend='Mexico ', name='Mexico')
p.xaxis.major_label_orientation = 0.90
p.legend.click_policy="hide"
p.add_tools(HoverTool(tooltips=[('Country', '$name'),
                                ('Date', '@Date'),
                                ('Value', '$y')]))

show(p)
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource, HoverTool
from bokeh.io import show, output_notebook
import pandas as pd
import numpy as np

index = ['2018-01-31', '2018-02-28', '2018-03-31', '2018-04-30', '2018-05-31', 
         '2018-06-30', '2018-07-31', '2018-08-31', '2018-09-30', '2018-10-31']

columns = ['Argentina', 'Turkey', 'Mexico']

np.random.seed(123)
data = np.random.rand(10, 3)
df = pd.DataFrame(index=index, columns=columns, data=data)
df_transposed = df.transpose()
source = ColumnDataSource({"xs": [df.index.values.tolist()]*len(list(df.columns.values)), "ys": df_transposed.values.tolist(), "colors": ["red", "green", "blue"], "names": list(df.columns.values)})

output_notebook()
p = figure(plot_height=400, x_range=index)
p.multi_line(xs='xs', ys='ys', color = "colors", name="names", legend="names", source=source)
p.xaxis.major_label_orientation = 0.90
p.legend.click_policy="hide"
p.add_tools(HoverTool(tooltips=[('Country', '@names'),
                                ('Date', '$x'),
                                ('Value', '$y')]))

show(p)
具有多行的版本:

from bokeh.plotting import figure
from bokeh.models import ColumnDataSource, HoverTool
from bokeh.io import output_notebook, show
import pandas as pd
import numpy as np

index = ['2018-01-31', '2018-02-28', '2018-03-31', '2018-04-30', '2018-05-31', 
         '2018-06-30', '2018-07-31', '2018-08-31', '2018-09-30', '2018-10-31']

columns = ['Argentina', 'Turkey', 'Mexico']

np.random.seed(123)
data = np.random.rand(10, 3)

df = pd.DataFrame(index=index, columns=columns, data=data)
df['Date'] = index

output_notebook() 
source = ColumnDataSource(df)

p = figure(plot_height=400, x_range=index)
p.line(x='Date', y='Argentina', source=source, color='red', legend='Argentina ', name='Argentina')
p.line(x='Date', y='Turkey', source=source, color='blue', legend='Turkey ', name='Turkey')
p.line(x='Date', y='Mexico', source=source, color='green', legend='Mexico ', name='Mexico')
p.xaxis.major_label_orientation = 0.90
p.legend.click_policy="hide"
p.add_tools(HoverTool(tooltips=[('Country', '$name'),
                                ('Date', '@Date'),
                                ('Value', '$y')]))

show(p)
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource, HoverTool
from bokeh.io import show, output_notebook
import pandas as pd
import numpy as np

index = ['2018-01-31', '2018-02-28', '2018-03-31', '2018-04-30', '2018-05-31', 
         '2018-06-30', '2018-07-31', '2018-08-31', '2018-09-30', '2018-10-31']

columns = ['Argentina', 'Turkey', 'Mexico']

np.random.seed(123)
data = np.random.rand(10, 3)
df = pd.DataFrame(index=index, columns=columns, data=data)
df_transposed = df.transpose()
source = ColumnDataSource({"xs": [df.index.values.tolist()]*len(list(df.columns.values)), "ys": df_transposed.values.tolist(), "colors": ["red", "green", "blue"], "names": list(df.columns.values)})

output_notebook()
p = figure(plot_height=400, x_range=index)
p.multi_line(xs='xs', ys='ys', color = "colors", name="names", legend="names", source=source)
p.xaxis.major_label_orientation = 0.90
p.legend.click_policy="hide"
p.add_tools(HoverTool(tooltips=[('Country', '@names'),
                                ('Date', '$x'),
                                ('Value', '$y')]))

show(p)
我对悬停工具只有一个问题。它没有显示正确的日期。(现在它显示了x位置,如果我将其替换为“xs”,它将显示整个日期列表)

xs和ys应该是一个列表列表,这就是它不起作用的原因。 我将多行替换为3条普通行,现在应该可以正常工作了。 如果您确实想使用多行,则应按如下方式格式化数据:

index = ['2018-01-31', '2018-02-28', '2018-03-31', '2018-04-30', '2018-05-31', 
         '2018-06-30', '2018-07-31', '2018-08-31', '2018-09-30', '2018-10-31']
source = ColumnDataSource(dict(
"xs": [index, index, index],
"ys": np.random.rand(3, 10).tolist()
)) 
使用行而不是多行处理代码:

from bokeh.plotting import figure
from bokeh.models import ColumnDataSource, HoverTool
from bokeh.io import output_notebook, show
import pandas as pd
import numpy as np

index = ['2018-01-31', '2018-02-28', '2018-03-31', '2018-04-30', '2018-05-31', 
         '2018-06-30', '2018-07-31', '2018-08-31', '2018-09-30', '2018-10-31']

columns = ['Argentina', 'Turkey', 'Mexico']

np.random.seed(123)
data = np.random.rand(10, 3)

df = pd.DataFrame(index=index, columns=columns, data=data)
df['Date'] = index

output_notebook() 
source = ColumnDataSource(df)

p = figure(plot_height=400, x_range=index)
p.line(x='Date', y='Argentina', source=source, color='red', legend='Argentina ', name='Argentina')
p.line(x='Date', y='Turkey', source=source, color='blue', legend='Turkey ', name='Turkey')
p.line(x='Date', y='Mexico', source=source, color='green', legend='Mexico ', name='Mexico')
p.xaxis.major_label_orientation = 0.90
p.legend.click_policy="hide"
p.add_tools(HoverTool(tooltips=[('Country', '$name'),
                                ('Date', '@Date'),
                                ('Value', '$y')]))

show(p)
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource, HoverTool
from bokeh.io import show, output_notebook
import pandas as pd
import numpy as np

index = ['2018-01-31', '2018-02-28', '2018-03-31', '2018-04-30', '2018-05-31', 
         '2018-06-30', '2018-07-31', '2018-08-31', '2018-09-30', '2018-10-31']

columns = ['Argentina', 'Turkey', 'Mexico']

np.random.seed(123)
data = np.random.rand(10, 3)
df = pd.DataFrame(index=index, columns=columns, data=data)
df_transposed = df.transpose()
source = ColumnDataSource({"xs": [df.index.values.tolist()]*len(list(df.columns.values)), "ys": df_transposed.values.tolist(), "colors": ["red", "green", "blue"], "names": list(df.columns.values)})

output_notebook()
p = figure(plot_height=400, x_range=index)
p.multi_line(xs='xs', ys='ys', color = "colors", name="names", legend="names", source=source)
p.xaxis.major_label_orientation = 0.90
p.legend.click_policy="hide"
p.add_tools(HoverTool(tooltips=[('Country', '@names'),
                                ('Date', '$x'),
                                ('Value', '$y')]))

show(p)
具有多行的版本:

from bokeh.plotting import figure
from bokeh.models import ColumnDataSource, HoverTool
from bokeh.io import output_notebook, show
import pandas as pd
import numpy as np

index = ['2018-01-31', '2018-02-28', '2018-03-31', '2018-04-30', '2018-05-31', 
         '2018-06-30', '2018-07-31', '2018-08-31', '2018-09-30', '2018-10-31']

columns = ['Argentina', 'Turkey', 'Mexico']

np.random.seed(123)
data = np.random.rand(10, 3)

df = pd.DataFrame(index=index, columns=columns, data=data)
df['Date'] = index

output_notebook() 
source = ColumnDataSource(df)

p = figure(plot_height=400, x_range=index)
p.line(x='Date', y='Argentina', source=source, color='red', legend='Argentina ', name='Argentina')
p.line(x='Date', y='Turkey', source=source, color='blue', legend='Turkey ', name='Turkey')
p.line(x='Date', y='Mexico', source=source, color='green', legend='Mexico ', name='Mexico')
p.xaxis.major_label_orientation = 0.90
p.legend.click_policy="hide"
p.add_tools(HoverTool(tooltips=[('Country', '$name'),
                                ('Date', '@Date'),
                                ('Value', '$y')]))

show(p)
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource, HoverTool
from bokeh.io import show, output_notebook
import pandas as pd
import numpy as np

index = ['2018-01-31', '2018-02-28', '2018-03-31', '2018-04-30', '2018-05-31', 
         '2018-06-30', '2018-07-31', '2018-08-31', '2018-09-30', '2018-10-31']

columns = ['Argentina', 'Turkey', 'Mexico']

np.random.seed(123)
data = np.random.rand(10, 3)
df = pd.DataFrame(index=index, columns=columns, data=data)
df_transposed = df.transpose()
source = ColumnDataSource({"xs": [df.index.values.tolist()]*len(list(df.columns.values)), "ys": df_transposed.values.tolist(), "colors": ["red", "green", "blue"], "names": list(df.columns.values)})

output_notebook()
p = figure(plot_height=400, x_range=index)
p.multi_line(xs='xs', ys='ys', color = "colors", name="names", legend="names", source=source)
p.xaxis.major_label_orientation = 0.90
p.legend.click_policy="hide"
p.add_tools(HoverTool(tooltips=[('Country', '@names'),
                                ('Date', '$x'),
                                ('Value', '$y')]))

show(p)

我对悬停工具只有一个问题。它没有显示正确的日期。(现在它显示了x位置,如果我将其替换为“xs”,它将显示整个日期列表)

谢谢Jasper,但这实际上对我不起作用。它不会生成工具提示。另外,我更喜欢使用multi_line.wird,在运行它时效果很好。可能它不起作用,因为我从答案中删除了输出_notebook(),因为我没有在笔记本中运行它,或者可能我们正在使用不同版本的Bokeh?我使用的是Bokeh1.0.1。无论如何,我再次添加了行,并添加了一个具有多行的版本。对于bokeh版本2.2.3,多行示例的工具提示不可见。在我的chrome控制台中,我得到了一个错误:“UncaughtTypeError:无法读取b处未定义的属性“2”。_update(bokeh.min.js?v=c77564ca8be6b5a7648d980fe180d62bd99ccfc01bd272a3e708b9533f34b1c86b24a8c86555f838fa26e42697aad84c411cac25f042231ed56f1eab:569)。”每当我滚动一行时@Jasper知道为什么它不起作用吗?在向多行图示符添加工具提示时出现了一个问题,但看起来您还发现。。。我在最新版本(2.2.3)中仍然存在此问题,因此我不确定如何修复此问题。谢谢你,贾斯珀,但这对我来说并不管用。它不会生成工具提示。另外,我更喜欢使用multi_line.wird,在运行它时效果很好。可能它不起作用,因为我从答案中删除了输出_notebook(),因为我没有在笔记本中运行它,或者可能我们正在使用不同版本的Bokeh?我使用的是Bokeh1.0.1。无论如何,我再次添加了行,并添加了一个具有多行的版本。对于bokeh版本2.2.3,多行示例的工具提示不可见。在我的chrome控制台中,我得到了一个错误:“UncaughtTypeError:无法读取b处未定义的属性“2”。_update(bokeh.min.js?v=c77564ca8be6b5a7648d980fe180d62bd99ccfc01bd272a3e708b9533f34b1c86b24a8c86555f838fa26e42697aad84c411cac25f042231ed56f1eab:569)。”每当我滚动一行时@Jasper知道为什么它不起作用吗?在向多行图示符添加工具提示时出现了一个问题,但看起来您还发现。。。我在最新版本(2.2.3)中仍然存在此问题,因此我不确定如何修复此问题。