当鼠标悬停在数据点上时显示 xy 数据点

时间:2021-06-28 04:22:19

标签: python pandas matplotlib

我想在悬停到特定数据点时显示图表的数据点(x 值、y 值)。
有谁知道如何根据我当前的代码实现它?

        ax = df.plot(x=1, y=2)  #x value from column 1 of pandas dataframe, 
                                #y value from column 2 of pandas dataframe

我的图表看起来像这样
enter image description here

1 个答案:

答案 0 :(得分:0)

以下是如何使用 bokeh 实现此目的的示例:

import matplotlib.pyplot as plt
import numpy as np; np.random.seed(1)
from bokeh.plotting import figure, show
from bokeh.io import output_notebook
from bokeh.models import HoverTool
from collections import OrderedDict


x = np.sort(np.random.rand(15))
y = np.sort(np.random.rand(15))
names = np.array(list("ABCDEFGHIJKLMNO"))

p = figure(plot_width=1000, plot_height=600,
           tools="pan,hover", 
           title="Changes",
           x_axis_label='height', 
           y_axis_label='Weight',
           toolbar_location="left"
          )

hover = p.select(dict(type=HoverTool))
hover.tooltips = OrderedDict([('height', '@x'), ('Weight', '$y')])

output_notebook()

p.line(x, y, legend="Weight")
p.line(x, y, legend="Height", line_color="red")

show(p)  

给予

enter image description here