【问题标题】:show the xy data point when the mouse over over the data point当鼠标悬停在数据点上时显示 xy 数据点
【发布时间】:2021-06-28 04:22:19
【问题描述】:

我想在悬停到特定数据点时显示图表的数据点(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

我的图表看起来像这样

【问题讨论】:

    标签: python pandas matplotlib


    【解决方案1】:

    以下是如何使用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)  
    

    给予

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-27
      • 1970-01-01
      相关资源
      最近更新 更多