【问题标题】:adding labels to candlestick chart in matplotlib在 matplotlib 中为烛台图添加标签
【发布时间】:2012-11-19 13:15:45
【问题描述】:

我正在尝试将一个系列(由 [1,2,0,....] 列表组成)添加到我使用 matplotlib 制作的烛台图表中,但无法弄清楚如何为每个烛台包含这些标签图表中的特定蜡烛。基本上我想制作一张这样的图表:


(来源:linnsoft.com

在每根蜡烛上方或下方带有带有数字(我的信号系列)的标签。 有什么办法可以达到吗?

不知道是否有帮助,但我的系列属于 pandas DataFrame 类型...

【问题讨论】:

    标签: pandas label candlestick-chart


    【解决方案1】:

    这是一个源自 - http://matplotlib.org/examples/pylab_examples/finance_demo.html的示例

    特别注意下面代码中的ax.annotate方法调用。

    from pylab import *
    from matplotlib.dates import  DateFormatter, WeekdayLocator, HourLocator, \
         DayLocator, MONDAY
    from matplotlib.finance import quotes_historical_yahoo, candlestick,\
         plot_day_summary, candlestick2
    
    # (Year, month, day) tuples suffice as args 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 = 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)
    #ax.xaxis.set_minor_formatter(dayFormatter)
    
    #plot_day_summary(ax, quotes, ticksize=3)
    candlestick(ax, quotes, width=0.6)
    
    ax.xaxis_date()
    ax.autoscale_view()
    setp( gca().get_xticklabels(), rotation=45, horizontalalignment='right')
    
    import datetime
    dt = datetime.datetime(2004, 3, 8)
    
    # Annotating a specific candle
    ax.annotate('This is my special candle', xy=(dt, 24), xytext=(dt, 25),
                arrowprops=dict(facecolor='black', shrink=0.05),
               )
    
    show()
    

    如果你运行这个文件,结果图应该会告诉你:-

    【讨论】:

    • 感谢您的回复。我已经看到了这段代码,但找不到一种简单的方法来为每根蜡烛显示一个特定标签(与另一个、相同长度、系列相关联)。我想知道是否有一种实际的方法可以做到这一点..
    • 我设法将您的代码包含在一个 for 循环中,现在它可以工作了......谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-04
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    • 2014-06-22
    • 1970-01-01
    相关资源
    最近更新 更多