Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用matplotlib在python中绘制OHLC图表_Python_Matplotlib - Fatal编程技术网

使用matplotlib在python中绘制OHLC图表

使用matplotlib在python中绘制OHLC图表,python,matplotlib,Python,Matplotlib,我有一个烛台对象列表,每个对象有6个值(打开、高、低、关闭、音量、时间戳)。我想使用matplotlib.finance.candlestick2_ohlc(ax,opens,high,low,closes,width=4,colorup='k',colordown='r',alpha=0.75)函数来绘制这些数据。问题是,如何将列表细分为打开、高、低和关闭,以便将其加载到此函数中 这是我的烛台课: class Candle: #Candlestick chart object def __in

我有一个烛台对象列表,每个对象有6个值(打开、高、低、关闭、音量、时间戳)。我想使用matplotlib.finance.candlestick2_ohlc(ax,opens,high,low,closes,width=4,colorup='k',colordown='r',alpha=0.75)函数来绘制这些数据。问题是,如何将列表细分为打开、高、低和关闭,以便将其加载到此函数中

这是我的烛台课:

class Candle:
#Candlestick chart object
def __init__(self, open, high, low, close, volume, timeStamp):
    self.open = open
    self.high = high
    self.low = low
    self.close = close
    self.volume = volume
    self.timestamp = timeStamp

def __str__(self):
    return """
    Open: %s
    High: %s
    Low: %s
    Close: %s
    Volume : %s
    Timestamp: %s""" %(self.open, self.high, self.low, self.close, self.volume, self.timestamp)
这是我列出的施工方法:

def getTradeHistory(self, timeFrame, symbol, count):
    #Get the trade history from the API

    return self.client.Trade.Trade_getBucketed(binSize=timeFrame, partial=True, symbol=symbol, reverse=False, count=count).result()

def constructCandles(self, th):
    #Iterate through list of trade history items and store them as candles in a list

    for candle in th :
        self.candles.append(Candle(candle['open'], candle['high'], candle['low'], candle['close'], candle['volume'], candle['timestamp']))

假设您的烛台对象列表称为my_Candies:

opens = [candle.open for candle in my_candles]
highs = [candle.high for candle in my_candles]
lows = [candle.low for candle in my_candles]
closes = [candle.close for candle in my_candles]
现在您有了开盘、收盘、高点和低点的列表,您可以调用matplotlib.finance.candlestick2_ohlc

import matplotlib.pyplot as plt
import matplotlib.finance as mpf

fig, ax = plt.subplots(figsize=(8,5))
mpf.candlestick2_ochl(ax, opens, closes, highs, lows, width=4, colorup='k', colordown='r', alpha=0.75)
还请注意,matplotlib.finance在2.0中已被弃用