如何使用pylab向动态绘图添加子图

2024-09-29 23:21:20 发布

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

我从来没有使用过pyplot,而且我是python的新手(使用2.7.3),我尝试过几种方法尝试在下面的类中添加2个子时隙,但我无法使其正常工作。我尝试过一些引用,比如http://matplotlib.org/examples/pylab_examples/shared_axis_demo.html和{a2} 但我不熟悉这些演示使用的语法,也不熟悉如何将其扩展到我修改过的类中。在

我需要一些更动态的工作,这样我就可以根据需要添加到数据集和回复。这是我处理绘图的基本类

class PlotTrades(object):
    '''Plot and save graph of Prices vs Buy/Sell'''
    def __init__(self, pair):
        self.pair = pair
        self.graph = pylab.figure()
        pylab.rcParams.update({'legend.labelspacing': 0.25,
                               'legend.fontsize': 'x-small'})
        self.tradeCount = 0;
        self.build()

    def build(self):
        self.toPlot = {}
        self.toPlot['price'] = {'label': 'Price', 'color': 'k', 'style': '-'}

    def append(self, line, value):
        '''Append new point to specified line['values'] in toPlot dict'''
        self.toPlot[line].setdefault('values', []).append(value)
        self.toPlot[line].setdefault('count', []).append(self.tradeCount)

    def updatePlot(self):
        '''Clear, re-draw, and save.
        Allows viewing "real-time" as an image
        '''
        self.tradeCount += 1

        # clear figure and axes
        pylab.clf()
        pylab.cla()
        pylab.grid(True, axis='y', linewidth=1, color='gray', linestyle='--')
        # plot each line
        pylab.plot(self.toPlot['price'].get('count'), self.toPlot['price'].get('values'), label='Price', color='k',
                   linestyle='-')

        pylab.ylim([0,10])
        # labels
        # legend top-left
        pylab.legend(loc=2)
        # save and close
        pylab.savefig('trade_graph.png')
        pylab.close(self.graph)

这个代码是一个情节,我怎么把它分成3个?我想添加一个2子批次的数据尚未显示在这段代码。我知道我需要使用子图和布局图,但是如何将正确的数据集与每个绘图关联起来?在

如果能朝着正确的方向努力,我们将不胜感激。在


Tags: and数据selfsavedeflinepricegraph

热门问题