【问题标题】:Mouse Hover in matplotlib candlestick for pythonpython的matplotlib烛台中的鼠标悬停
【发布时间】:2012-08-02 14:15:01
【问题描述】:

我已经实现了这个例子:

http://matplotlib.sourceforge.net/examples/pylab_examples/finance_demo.html?highlight=candlestick

我想为烛台实现鼠标悬停功能,以便我可以在弹出窗口或某个面板中的标签中看到烛台的开盘/高/低/收盘。我跟着:

http://matplotlib.sourceforge.net/examples/event_handling/pick_event_demo.html

不幸的是,函数:

candlestick(ax, quotes, width=0.6)

没有picker=True 关键字。有没有其他方法可以实现鼠标悬停功能?

matplotlib.finance 的文档 http://doc.astro-wise.org/matplotlib.finance.html

【问题讨论】:

    标签: python events matplotlib finance


    【解决方案1】:

    我已经提供了一个链接到一些有价值信息的答案。因为你想通过查看数据而不是艺术家来处理事情,所以我想我会给你更多的帮助。我所做的是采用 matplotlib 烛台示例,并添加了一个鼠标移动事件,该事件打印鼠标当前结束的日期以及 3 个最近(及时)的股票。从这一点来看,只需将两个答案结合起来即可产生所需的结果:

    #!/usr/bin/env python
    import matplotlib.pyplot as plt
    import pylab
    from matplotlib.dates import  DateFormatter, WeekdayLocator, HourLocator, \
         DayLocator, MONDAY, num2date
    from matplotlib.finance import quotes_historical_yahoo, candlestick,\
         plot_day_summary, candlestick2
    
    # (Year, month, day) tuples suffice as aregs for quotes_historical_yahoo
    date1 = ( 2004, 2, 1)
    date2 = ( 2004, 4, 12 )
    
    
    mondays = WeekdayLocator(MONDAY)        # major ticks on the mondays
    alldays    = DayLocator()              # minor ticks on the days
    weekFormatter = DateFormatter('%b %d')  # Eg, Jan 12
    dayFormatter = DateFormatter('%d')      # Eg, 12
    
    quotes = quotes_historical_yahoo('INTC', date1, date2)
    if len(quotes) == 0:
        raise SystemExit
    
    fig = plt.figure()
    fig.subplots_adjust(bottom=0.2)
    ax = fig.add_subplot(111)
    ax.xaxis.set_major_locator(mondays)
    ax.xaxis.set_minor_locator(alldays)
    ax.xaxis.set_major_formatter(weekFormatter)
    
    candlestick(ax, quotes, width=0.6)
    
    ax.xaxis_date()
    ax.autoscale_view()
    plt.setp(ax.get_xticklabels(), rotation=45, horizontalalignment='right')
    
    
    def on_move(event):
        ax = event.inaxes
        if ax is not None:
            # convert x y device coordinates to axes data coordinates
            date_ordinal, y = ax.transData.inverted().transform([event.x, event.y])
    
            # convert the numeric date into a datetime
            date = num2date(date_ordinal)
    
            # sort the quotes by their distance (in time) from the mouse position
            def sorter(quote):
                return abs(quote[0] - date_ordinal)
            quotes.sort(key=sorter)
    
            print 'on date %s the nearest 3 openings were %s at %s respectively' % \
                            (date, 
                             ', '.join([str(quote[1]) for quote in quotes[:3]]),
                             ', '.join([str(num2date(quote[0])) for quote in quotes[:3]]))
    
    
    on_move_id = fig.canvas.mpl_connect('motion_notify_event', on_move)
    
    
    plt.show()
    

    HTH

    【讨论】:

    • 感谢您的帮助。我修改了你原来的想法,让它按我想要的方式工作。与这个非常相似,只是我事先将其存储为字典以便于查找。
    【解决方案2】:

    我写了一个答案来处理“鼠标悬停”事件here。这将是了解如何做类似事情的一个很好的第一点。

    【讨论】:

    • 对不起,我现在追得更好了。有什么方法可以让注释/文本在面板中弹出?我可能有数千个条形图,因此当我将鼠标悬停在它们上方时,我不希望遍历所有条形图以检查可见性。
    • 是否有另一种方法来检查是否包含“事件”?根据doc.astro-wise.org/matplotlib.finance.html,我的数据列表只是 [datetime, open, high, low, close, volume] 的元组
    猜你喜欢
    • 1970-01-01
    • 2011-12-01
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    • 2014-07-01
    • 2011-08-04
    • 2018-09-03
    • 1970-01-01
    相关资源
    最近更新 更多