从下面的Pandas数据框中,我只是试图创建一个多行图和HoverTool;但是,我找不到与我的具体情况类似的任何示例。这是我的示例代码:
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)
答案 0 :(得分:1)
Multi lines 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)
我只有一个关于悬停工具的问题。它没有显示正确的日期。 (现在它显示了x位置,如果我将其替换为“ xs”,它将显示整个日期列表)