在matplotlib添加标签到烛图(adding labels to candlestick ch

2019-11-01 05:45发布

我想添加一系列的清单(组成[1,2,0,...]),以阴阳烛图我matplotlib生产,但不能制定出如何将每个特定的蜡烛在这些标签图形。 基本上我想产生这样一个图表:


(来源: linnsoft.com )

与数字(我的信号系列)刚刚超过或低于每个蜡烛的标签。 有没有什么办法可以达到吗?

不知道是否有帮助,但我的系列是大熊猫据帧样的...

Answer 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()

所得的情节,如果你运行这个文件,应该告诉你: -



文章来源: adding labels to candlestick chart in matplotlib