如何在python中添加音量条形图?

时间:2018-05-06 19:18:11

标签: python finance candlestick-chart

你好我这个代码在Bittrex上查看加密货币上的烛台图表,但我想添加该卷。我怎样才能做到这一点 ?

在我的代码的开头,我从JSON URL获取数据,这允许我拥有烛台图表,当我获取数据时,我将卷保存在 float(data['result'][i]['V'])但我不知道如何绘制音量...

这是我的代码:

import urllib2
import json
from matplotlib.finance import candlestick2_ohlc
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import datetime as datetime
import numpy as np

l = []
date = []
date_intermediaire = []

response = urllib2.urlopen('https://bittrex.com/Api/v2.0/pub/market/GetTicks?marketName=BTC-ETH&tickInterval=thirtyMin&_=')
data = json.load(response)
print data


for i in range(len(data['result'])-95,len(data['result'])):
    append_me = float(data['result'][i]['O']), float(data['result'][i]['H']), float(data['result'][i]['L']), float(data['result'][i]['C']), float(data['result'][i]['V'])
    l.append((append_me))
    date.append(data['result'][i]['T'])

ohlc = np.array(l, dtype=[('open', '<f4'), ('high', '<f4'), ('low', '<f4'), ('close', '<f4'), ('volume', '<f4')])


for i in range(0,len(date)):
    date[i] = datetime.datetime.strptime(date[i], "%Y-%m-%dT%H:%M:%S")
    date_intermediaire.append((date[i]-datetime.datetime(1970,1,1)).total_seconds())




xdate = [datetime.datetime.fromtimestamp(i) for i in date_intermediaire]




fig, ax = plt.subplots()
candlestick2_ohlc(ax,ohlc['open'],ohlc['high'],ohlc['low'],ohlc['close'],width=0.6)



ax.xaxis.set_major_locator(ticker.MaxNLocator(6))


def mydate(x,pos):
    try:
        return xdate[int(x)]
    except IndexError:
        return ''


ax.xaxis.set_major_formatter(ticker.FuncFormatter(mydate))

fig.autofmt_xdate()
fig.tight_layout()
plt.savefig("test.png")

到目前为止,这是我的代码的结果:

candlesticks

2 个答案:

答案 0 :(得分:0)

  

我想添加音量。我该怎么做?

添加&#34;辅助&#34; Y轴+绘制相对于它的音量

这是如何在同一布局上安全设置两个不同图形的叠加的标准方法。

ax2 = ax1.twinx() # this setting does the job

以这种方式检查details in documentation and examplesenter image description here

如下所述:

要么&#34;撰写&#34;两个子图的无间隙布局,或者更好地设置强制缩放,以便共享X轴基础。

enter image description here

pyplot工具允许这样做 比如ax2 = fig1.add_subplot( 111, sharex = ax1, frameon = False )
非常好用多种方式,只需继续调整设置并打磨ax1ax2轴刻度和标签,以最好地满足您的口味)

答案 1 :(得分:0)

ax1 = plt.subplot2grid((6,1), (0,0), rowspan=5, colspan=1)
ax2 = plt.subplot2grid((6,1), (5,0), rowspan=1, colspan=1, sharex=ax1)

方便的图表分割方法,您可以将音量置于底部,蜡烛置于顶部