实时烛台图表(matplotlib)

时间:2013-09-25 15:16:21

标签: python matplotlib

我正在尝试使用Interactive Brokers的数据实时绘制matplotlib.finance的烛台图表。有没有人有一个简单的例子呢?我可以使用简单的线条图来完成它,其中包含

的内容
class Data:
    def __init__(self, maxLen=50):
        self.maxLen = maxLen
        self.x_data = np.arange(maxLen)
        self.y_data = deque([], maxLen)

    def add(self, val):
        self.y_data.append(val)

class Plot:
    def __init__(self, data):
        plt.ion()
        self.ayline, = plt.plot(data.y_data)
        self.ayline.set_xdata(data.x_data)

    def update(self, data):
        self.ayline.set_ydata(data.y_data)
        plt.draw()

if __name__ == "__main__":
    data = Data()
    plot = Plot(data)

    while 1:
        data.add(np.random.randn())
        plot.update(data)
        sleep(1)

如何通过提供5元组作为y值来将其更改为烛台图表?

1 个答案:

答案 0 :(得分:6)

我刚才有类似的工作。为了更简单地说明该方法,我修改了matplotlib站点中的finance_demo示例。

#!/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


# make plot interactive in order to update
plt.ion()

class Candleplot:
    def __init__(self):
        fig, self.ax = plt.subplots()
        fig.subplots_adjust(bottom=0.2)

    def update(self, quotes, clear=False):

        if clear:
            # clear old data
            self.ax.cla()

        # axis formatting
        self.ax.xaxis.set_major_locator(mondays)
        self.ax.xaxis.set_minor_locator(alldays)
        self.ax.xaxis.set_major_formatter(weekFormatter)

        # plot quotes
        candlestick(self.ax, quotes, width=0.6)

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

        # use draw() instead of show() to update the same window
        plt.draw()


# (Year, month, day) tuples suffice as args for quotes_historical_yahoo
date1 = ( 2004, 2, 1)
date2 = ( 2004, 4, 12 )
date3 = ( 2004, 5, 1 )

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)

plot = Candleplot()
plot.update(quotes)

raw_input('Hit return to add new data to old plot')

new_quotes = quotes_historical_yahoo('INTC', date2, date3)

plot.update(new_quotes, clear=False)

raw_input('Hit return to replace old data with new')

plot.update(new_quotes, clear=True)

raw_input('Finished')

基本上,我使用plt.ion()打开交互模式,以便在程序继续运行时更新绘图。要更新数据,似乎有两种选择。 (1)您可以使用新数据再次调用烛台(),这会将其添加到绘图中,而不会影响先前绘制的数据。这可能更适合在最后添加一个或多个新蜡烛;只需传递包含新蜡烛的列表。 (2)在传递新数据之前,使用ax.cla()(清除轴)删除所有先前的数据。如果你想要一个移动窗口,例如,这将是更好的选择。仅绘制最后50支蜡烛,因为只是在最后添加新蜡烛将导致越来越多的蜡烛积累在情节中。同样,如果要在关闭之前更新最后一根蜡烛,则应首先清除旧数据。清除轴也会清除一些格式,因此应该设置函数以在调用ax.cla()之后重复轴的格式化。

此时不确定问题是否与原始海报相关,但希望这对某人有帮助。