【问题标题】:Tooltips/Hover over shows ??? - python/bokeh工具提示/悬停在节目上??? - 蟒蛇/散景
【发布时间】:2021-12-29 18:40:55
【问题描述】:

我正在使用 python/bokeh 绘制具有某些属性的点,现在我遇到了一个我无法解决的问题:悬停工具提示不适用于某些列。

考虑到我的这行代码:

    HOVER_TOOLTIPS = [("index", "$index"),("(x,y)", "($x, $y)"), ("weight","@weight"),("height","@height")]

我想要做的是:当我通过鼠标时它显示给我,每个点的属性、索引、x 和 y 位置工作正常,但有关重量和高度列的信息显示为' ???'。我做错了什么?

我的数据是什么样的:

               Names   weight (kg)  height (m)  x    y
0             Ledger         89.3       1.800  512  725
1           Nicholas        102.3       1.750  618  884
2               Axel         75.0       1.680  725  538
3                Kai         83.7       1.970  578  604
4               Lyle         78.4       1.660  605  586
5               Jair         66.1       1.830  621  817
6             Flynn          82.0       1.690  632  837
7             Baylor         96.0       1.730  608  786
8               Niko         89.5       1.850  628  797
9               Kian         71.2       1.760  710  674
10             Jonas         88.9       1.900  615  779
11             Rhett         65.8       1.880  820  766
12             Asher         93.8       1.560  719  615
13             Elian         56.3       1.640  830  889
14             Alora         60.2       0.165  602  635
15             Mylah         52.9       1.600  614  859

My complete code:
````````````````````````````````````````````````````````````````````````
from bokeh.plotting import figure
from bokeh.models import GraphRenderer, Ellipse
from bokeh.palettes import Spectral8
from bokeh.models import ColumnDataSource, GlyphRenderer, GraphRenderer, StaticLayoutProvider, Circle, CDSView, IndexFilter, Button
import pandas
import networkx
import matplotlib.pyplot as plt
import numpy as np
from bokeh.models.graphs import from_networkx
from bokeh.io import output_notebook, show, save
from bokeh.models import Range1d, Circle, ColumnDataSource, MultiLine
output_notebook()

df = pandas.read_excel('Graph.xlsx', engine='openpyxl')

count_rows = len(df)

weight = df['weight (kg)']
height = df['height (m)']

node_indices = list(range(count_rows))

HOVER_TOOLTIPS = [("index", "$index"),("(x,y)", "($x, $y)"), ("weight","@weight"),("height","@height")]

plot = figure(tooltips = HOVER_TOOLTIPS,
              tools="pan,wheel_zoom,save,reset", active_scroll='wheel_zoom',
            x_range=Range1d(-600, 600), y_range=Range1d(640, 790), title='Network')

graph = GraphRenderer()

graph.node_renderer.glyph = Circle(size=15, fill_color='skyblue')

graph.node_renderer.data_source.data = dict(index=node_indices)

x = df['x'].tolist()
y = df['y'].tolist()

graph.edge_renderer.data_source.data = dict(start=[0]*count_rows,end=node_indices)

graph_layout = dict(zip(node_indices,zip(x, y)))

graph.layout_provider = StaticLayoutProvider(graph_layout=graph_layout)

plot.renderers.append(graph)

show(plot)

【问题讨论】:

  • 您永远不会在上述代码中的任何位置为节点渲染器的ColumnDataSource 添加"weight""height" 列。您添加 "index" 使其显示出来,$x$y(带有 $)是显示 鼠标位置 x 和 y 值的特殊变量,它们也将总是出现。

标签: python pandas hover tooltip bokeh


【解决方案1】:

问题是当您定义未使用ColumnDataSource 或dict 等的源数据时。您直接逐列获取数据并显示在图表中。所以,你应该创建一个数据源。

您的来源是:

{0: (512, 725), 1: (618, 884), 2: (725, 538)} # long version

这里没有身高、体重等,只有索引和 x/y 坐标。

所以你应该添加:

graph.node_renderer.data_source.data['weight'] = weight.tolist()
graph.node_renderer.data_source.data['height'] = height.tolist()

完整代码:

from bokeh.plotting import figure
from bokeh.models import GraphRenderer, Ellipse
from bokeh.palettes import Spectral8
from bokeh.models import ColumnDataSource, GlyphRenderer, GraphRenderer, StaticLayoutProvider, Circle, CDSView, IndexFilter, Button
import pandas as pd
import networkx
import matplotlib.pyplot as plt
import numpy as np
from bokeh.models.graphs import from_networkx
from bokeh.io import output_notebook, show, save
from bokeh.models import Range1d, Circle, ColumnDataSource, MultiLine


df = pd.DataFrame({'Names': ['Ledger', 'Nicholas', 'Axel'], 'weight (kg)': [89.3, 102.3, 75.0],
                  'height (m)': [1.800, 1.750, 1.680], 'x': [512, 618, 725], 'y':[725, 884, 538]})

count_rows = len(df)

weight = df['weight (kg)']
height = df['height (m)']

node_indices = list(range(count_rows))

HOVER_TOOLTIPS = [("index", "$index"),("(x,y)", "($x, $y)"), ("weight","@weight"),("height","@height")]

plot = figure(tooltips = HOVER_TOOLTIPS,
              tools="pan,wheel_zoom,save,reset", active_scroll='wheel_zoom',
            x_range=Range1d(-600, 600), y_range=Range1d(640, 790), title='Network')

graph = GraphRenderer()

graph.node_renderer.glyph = Circle(size=15, fill_color='skyblue')

graph.node_renderer.data_source.data = dict(index=node_indices)

x = df['x'].tolist()
y = df['y'].tolist()

data=dict(start=[0]*count_rows,end=node_indices)

graph.edge_renderer.data_source.data = data
graph.node_renderer.data_source.data['weight'] = weight.tolist()
graph.node_renderer.data_source.data['height']= height.tolist()


graph_layout = dict(zip(node_indices, zip(x, y)))

graph.layout_provider = StaticLayoutProvider(graph_layout=graph_layout)

plot.renderers.append(graph)

show(plot)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-14
    • 1970-01-01
    • 1970-01-01
    • 2015-05-07
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    相关资源
    最近更新 更多