将悬停工具提示添加到通过循环进行的Bokeh折线图中

时间:2019-01-18 03:45:20

标签: python bokeh

我使用“ for循环”制作了散景图。但是此方法阻止我添加工具提示,因为对悬停元组使用@方法会阻止我添加列名(如果它是循环的话)。有什么办法可以在“ for循环”中将每个国家的值和名称添加到我的工具提示中?下面的#悬停行不起作用。

import pandas as pd
url = 'https://www.bp.com/content/dam/bp/business-sites/en/global/corporate/xlsx/energy-economics/statistical-review/bp-stats-review-2018-all-data.xlsx'
df = pd.read_excel(url, sheet_name = 'Gas Consumption - Bcf', skiprows = 2, skipfooter = 15)
df = df.dropna(how='all').transpose()
df = df.rename(columns=df.iloc[0]).drop(df.index[0])
df = df.reset_index()
df.rename(columns = {'index': 'Year'}, inplace=True)
df = df.drop(df.index[[53, 54, 55]])
df['Year'] = pd.to_datetime(df['Year'], format = '%Y')

top_ten = df.tail(1).T.reset_index().iloc[1:,:]
top_ten.columns = ['country', 'value']
top_ten = top_ten.sort_values(by = 'value', ascending= False)
top_ten_list = top_ten['country'].tolist()
top_ten_list = [x for x in top_ten_list if not 'Total' in x][0:10]

from bokeh.plotting import figure, output_notebook, show, reset_output
from bokeh.models import ColumnDataSource
from bokeh.palettes import Category10
from bokeh.models import HoverTool
import itertools
from bokeh.models import Legend

mypalette = Category10[10]
output_notebook()

q = figure(plot_width=700, plot_height=500, x_axis_type='datetime')

for c, color in zip(top_ten_list, mypalette):
    q.line(df['Year'],df[c], legend=c, color = color, line_width = 3)
    #hover = HoverTool(tooltips = [('Date', '@Year{%Y}'), ('Country', '@c billion cubic feet per day')], formatters = {'Year' : 'datetime'})

q.add_tools(hover)
q.legend.location = "top_left"
q.xaxis.axis_label = "Date"
q.yaxis.axis_label = "billion cubic feet per day"
q.legend.click_policy="hide"
show(q)

1 个答案:

答案 0 :(得分:0)

我用ColumnDataSource和多行替换了for循环,这使得添加悬浮工具变得容易。我还必须添加一些CustomJS,因为从多行调用@ x / @ y会显示所有x / y值。 CustomJS确保仅显示正确的x / y位置。

import pandas as pd
from bokeh.plotting import figure, show, reset_output, output_notebook
from bokeh.models import ColumnDataSource
from bokeh.palettes import Category10
from bokeh.models import HoverTool, ColumnDataSource
from bokeh.models.glyphs import MultiLine
import itertools
from bokeh.models import Legend
from bokeh.models.tools import CustomJSHover

url = 'https://www.bp.com/content/dam/bp/business-sites/en/global/corporate/xlsx/energy-economics/statistical-review/bp-stats-review-2018-all-data.xlsx'
df = pd.read_excel(url, sheet_name = 'Gas Consumption - Bcf', skiprows = 2, skipfooter = 15)
df = df.dropna(how='all').transpose()
df = df.rename(columns=df.iloc[0]).drop(df.index[0])
df = df.reset_index()
df.rename(columns = {'index': 'Year'}, inplace=True)
df = df.drop(df.index[[53, 54, 55]])

top_ten = df.tail(1).T.reset_index().iloc[1:,:]
top_ten.columns = ['country', 'value']
top_ten = top_ten[~top_ten.country.str.contains("Total")]
top_ten = top_ten.sort_values(by = 'value', ascending= False)
top_ten_list = top_ten['country'].tolist()[:10]
top_ten = df[top_ten_list]

y = [df[country].tolist() for country in top_ten.columns.tolist()]
x, xLst = [], df['Year'].tolist()
for i in range(10):
    x.append(xLst)

x_custom = CustomJSHover(code="""
    return '' + special_vars.data_x
""")

y_custom = CustomJSHover(code="""
    return '' + special_vars.data_y
""")

data = {'x': x, 'y': y, 'color': Category10[10], 'name': top_ten_list}

source = ColumnDataSource(data)

output_notebook()

q = figure(plot_width=700, plot_height=500)

q.multi_line(xs='x', ys='y', line_color='color', legend='name', line_width = 3, source=source)
q.add_tools(HoverTool(
    tooltips=[
    ('Year', '@x{custom}'),
    ('Value', '@y{custom}'),
    ('Country', '@name')],
    formatters=dict(x=x_custom, y=y_custom)
))

q.legend.location = "top_left"
q.xaxis.axis_label = "Date"
q.yaxis.axis_label = "billion cubic feet per day"
q.legend.click_policy="hide"
show(q)