4

我想在 Matplotlib 上创建一个烛台图。我在网上找到了很多例子,但到目前为止,它们都在使用与 yahoo Finance 或其他类型数据的连接,但没有很好地解释当你有一个包含日期、打开、打开的元组列表时如何获得相同的结果收盘价,最高价和最低价。实际上,通常情况下,您已经有了历史值或估计值,或者更一般地说,您只是不想使用来自雅虎财经等提供商的数字。我想知道的是使用您自己的值列表创建烛台图之类的非常基本的代码。假设我有一个包含两天所需数据的元组列表:

Prices = [('01/01/2010', 1.123 (open), 1.212 (close), 1.463 (high), 1.056(low)),
          ('02/01/2010', 1.121 (open), 1.216 (close), 1.498 (high), 1.002(low))] 

我应该用这两个数据点准确编码以获得烛台图(这意味着列表“价格”的每个元素都在创建烛台的图)?我当然可以操作数据(例如要在浮点数中转换的日期字符串等),但我无法获得创建图表的简单命令。任何人都可以帮忙吗?

4

1 回答 1

5

在 matplotlib 示例之后,我得到了以下解决方案:

from pylab import *
import matplotlib.pyplot as plt
from datetime import datetime
import time
from matplotlib.dates import  DateFormatter, WeekdayLocator, HourLocator, \
     DayLocator, MONDAY
from matplotlib.finance import candlestick,\
     plot_day_summary, candlestick2


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

#starting from dates expressed as strings...
Date1 = '01/01/2010'
Date2 = '02/01/2010'
#...you convert them in float numbers....
Date1 = date2num(datetime.strptime(Date1, "%d/%m/%Y"))
Date2 = date2num(datetime.strptime(Date2, "%d/%m/%Y"))
#so redefining the Prices list of tuples...
Prices = [(Date1, 1.123, 1.212, 1.463, 1.056), (Date2,1.121, 1.216, 1.498, 1.002)]
#and then following the official example. 
fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.2)
ax.xaxis.set_major_locator(mondays)
ax.xaxis.set_minor_locator(alldays)
ax.xaxis.set_major_formatter(weekFormatter)
candlestick(ax, Prices, width=0.6)

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

plt.show()
于 2014-01-14T12:38:08.677 回答