烛台上的未来注解

2024-06-01 05:04:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有画烛台图的手稿。我在这个脚本中添加了两个注释。一个是“最后的数据”,显示最后一个蜡烛,第二个注释应该显示将来的点。但由于某种原因,它在照片上看不到。我怎样才能让它看得见?我想改变图表宽度plt.轴但没有找到解决方案。在

#!/usr/bin/env python
import matplotlib.pyplot as plt
from matplotlib.dates import  DateFormatter, WeekdayLocator, HourLocator, \
     DayLocator, MONDAY
from matplotlib.finance import quotes_historical_yahoo, candlestick,\
     plot_day_summary, candlestick2

from matplotlib.patches import Ellipse, Circle
el = Ellipse((2, -1), 0.5, 0.5)

# (Year, month, day) tuples suffice as args for quotes_historical_yahoo
date1 = ( 2013, 11, 10)
date2 = ( 2013, 12, 29)


mondays = WeekdayLocator(MONDAY)        # major ticks on the mondays
alldays    = DayLocator()              # minor ticks on the days
weekFormatter = DateFormatter('%b %d')  # e.g., Jan 12
dayFormatter = DateFormatter('%d')      # e.g., 12

quotes = quotes_historical_yahoo('INTC', date1, date2)

if len(quotes) == 0:
    raise SystemExit

fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.2)

candlestick(ax, quotes, width=0.6)

ax.xaxis_date()
plt.setp( plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')

dt = quotes[0][0]

ax.annotate(
                'Last data',
                xy=(dt+16, 24),
                xytext=(dt+3, 25),
                arrowprops=dict(arrowstyle='simple', fc="0.6", ec="none", patchB=el, connectionstyle="arc3,rad=0.3", facecolor='black')
        )

ax.annotate(
                'Future',
                xy=(dt+19, 24.5),
                xytext=(dt+2, 23.7),
                arrowprops=dict(arrowstyle='simple', fc="0.6", ec="none", patchB=el, connectionstyle="arc3,rad=0.3", facecolor='black')
        )

plt.savefig('test.png')

annotations


Tags: fromimportmatplotlibasdtpltaxel
3条回答

您只需更改图表上的xlimit:

ax = plt.gca()
ax.set_xlim([start_data, end_date])
plt.draw()

编写的代码适合我在OSX、Python2.7.5上使用。在

绘制并标记“未来”和“最后数据”弧。在

如果你数一数烛台,只有13个——如果你加上一个打印声明,你也会看到:

quotes = quotes_historical_yahoo('INTC', date1, date2)
print len(quotes)

正如您的代码所示,11月28日之后的所有内容都是“未来日期”,因此没有相关数据,因此从quotes\u historical_yahoo()返回的报价只有13天(加上周末的一些空白)。在

不能对11月28日之后的点进行注释,因为它们不在绘制的数据中,因此调用失败。在

可以将此代码添加为解决方法:

line = Line2D(
        xdata=(dt+19, dt+19),
        ydata=(24.5, 24.5), )

ax.add_line(line) ax.autoscale_view()

现在可以创建批注:

enter image description here

相关问题 更多 >