2014-03-31 67 views
1

下面的代码片段是用4个价格条创建烛台图表。在“NOT WORKING”标签之间写入的代码行应该在存储到变量d(x轴)和h(y轴)的坐标之后在第二个价格栏上标注单词“BUY”。但是,这不起作用,因为图表中没有注释。在matplotlib烛台图表上注释一些东西

下面的代码是可运行的,任何人都可以解释我如何在图表上做一个注释吗?

from pylab import * 
from matplotlib.finance import candlestick 
import matplotlib.gridspec as gridspec 

quotes = [(734542.0, 1.326, 1.3287, 1.3322, 1.3215), (734543.0, 1.3286, 1.3198, 1.3292, 1.3155), (734546.0, 1.321, 1.3187, 1.3284, 1.3186), (734547.0, 1.3186, 1.3133, 1.3217, 1.308)] 

fig, ax = subplots() 
candlestick(ax,quotes,width = 0.5) 
ax.xaxis_date() 
ax.autoscale_view() 

#NOT WORKING 
h = quotes[1][3] 
d = quotes[1][0] 
ax.annotate('BUY', xy = (d-1,h), xytext = (d-1, h+0.5), arrowprops = dict(facecolor='black',width=1,shrink=0.25)) 
#NOT WORKING  

plt.show() 

P.S.嵌入语句print "(", d, ",", h, ")"给出以下输出:>>> (734543.0 , 1.3292)。这正是我希望得到我的箭的地方,所以我猜这个问题必须与箭的形象化有关,而不是与它的创造有关。

回答

0

你的问题是你的箭头实际上不在matplotlib屏幕上。您已将xytext的位置设置为(d-1, h+0.5),这是您的方式y-limits。以下修复代码:

from pylab import * 
from matplotlib.finance import candlestick 
import matplotlib.gridspec as gridspec 

quotes = [(734542.0, 1.326, 1.3287, 1.3322, 1.3215), (734543.0, 1.3286, 1.3198, 1.3292, 1.3155), (734546.0, 1.321, 1.3187, 1.3284, 1.3186), (734547.0, 1.3186, 1.3133, 1.3217, 1.308)] 

fig, ax = subplots() 
candlestick(ax,quotes,width = 0.5) 
ax.xaxis_date() 
ax.autoscale_view() 

#NOT WORKING 
h = quotes[1][3] 
d = quotes[1][0] 
ax.annotate('BUY', xy = (d-1,h), xytext = (d-1, h+0.003), arrowprops = dict(facecolor='black',width=1,shrink=0.25)) 
#NOT WORKING  

plt.show() 

Plot output