可以在Python matplotlib上绘制Bittrex的BTC / USD价格标记,但不能用线条连接标记

时间:2017-10-19 14:58:15

标签: python matplotlib graph bitcoin

我有以下代码显示:

BTC/USD plot from Bittrex

然而,无论我尝试过什么,我都无法用线条加入标记!我想用一条线连接标记并且有一条细的水平线(在代码中注释掉)显示与绘制线相关的当前价格。我遇到的问题是水平线需要ax1.clear(),但它只显示一个标记作为点。

以下是代码:

#!/usr/local/bin/python3

import asyncio

import os
import sys

import datetime
import time

root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.append(root)

import ccxt.async as ccxt  # noqa: E402

import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import style


style.use('fivethirtyeight')

import matplotlib.dates as mdate
from matplotlib.ticker import FormatStrFormatter


fig = plt.figure()
ax1 = fig.add_subplot(1, 1, 1)
#fig, ax1 = plt.subplots()

fig.subplots_adjust(left = 0.1, bottom = 0.1)
#fig.tight_layout()

def animate(i):
    btcusd = (asyncio.get_event_loop().run_until_complete(ccxt.bittrex().fetch_ticker('BTC/USDT')))
    print(btcusd)
    print(btcusd['bid'])
    print(btcusd['ask'])

#    try:
#        startdate
#    except NameError:
#        startdate = mdate.epoch2num(btcusd['timestamp'] / 1000)

    xaxis = mdate.epoch2num(btcusd['timestamp'] / 1000)
    yaxis = btcusd['bid']
#    x = datetime.datetime.fromtimestamp(btcusd['timestamp'].astype(str))
    ######ax1.clear()
    #ax1.set_xticklabels(xs)
    #ax1.axis([0, 20, 0, 20])
    ##ax1.plot_date(x, btcusd['bid'])
    #####ax1.plot_date(x = xaxis, y = yaxis, lw=1, linestyle = 'solid') #fmt = '.r-') #, linestyle='solid', marker='None') #, fmt='-', linewidth=2)
    ax1.plot_date(xaxis, yaxis, ls='-', marker='.')

    ##ax1.yaxis.set_major_formatter(FormatStrFormatter('%.8f'))
    plt.axes().tick_params(labelsize=6)
    plt.axes().yaxis.set_major_formatter(FormatStrFormatter('%.2f'))
    ### ETH/BTC plt.axes().yaxis.set_major_formatter(FormatStrFormatter('%.8f'))
    plt.axes().xaxis.set_major_formatter(mdate.DateFormatter('%Y-%m-%d %H:%M:%S'))
    plt.axes().xaxis_date()
    plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='center')

    #ax1.plot(btcusd['bid'])
    plt.xlim(mdate.epoch2num((btcusd['timestamp'] / 1000) - 300), mdate.epoch2num((btcusd['timestamp'] / 1000) + 300))
    ##plt.xlim(startdate, datetime.date.today())
    # ETC/BTC plt.ylim(btcusd['bid'] - 0.0000001, btcusd['bid'] + 0.0000001)
    plt.ylim(btcusd['bid'] - 50, btcusd['bid'] + 50)
    ##ax1.plot()
#    plt.xscale('linear')
#    plt.yscale('linear')
    #plt.grid(True)

    #plt.set_data(x[:num], y[:num])
    #plt.axes.axis([-20, 20, -20, 20])    

##    ax1.axhline(y = btcusd['bid'], color = 'r', linestyle = '-', linewidth = 0.1)

# plt.grid(False)
#ax1.autoscale_view(True,True,True)
fig.autofmt_xdate()
ani = animation.FuncAnimation(fig, animate, interval=30000)
plt.show()

0 个答案:

没有答案