【发布时间】: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