如何从日志文件中绘制数据图形?

时间:2020-08-02 23:18:06

标签: python matplotlib

我有一个包含交易摘要的日志文件,该日志文件如下所示

date        time       amt     sell/buy     type    current_price 
2020-08-03 01:20:06    1.5    0.00187130    SELL    0.00187030
2020-08-03 01:23:20    1.1    0.00187110    SELL    0.00187090
2020-08-03 01:26:36    1.8    0.00187190    SELL    0.00187070
2020-08-03 01:29:52    1.3    0.00187160    SELL    0.00186820
2020-08-03 01:36:24    1.4    0.00186580    BUY    0.00186780
2020-08-03 01:39:41    1.4    0.00186860    SELL    0.00186640
2020-08-03 01:42:54    1.3    0.00186710    BUY    0.00186880
2020-08-03 01:46:10    1.0    0.00186990    SELL    0.00186980
2020-08-03 01:49:25    1.2    0.00186840    BUY    0.00186850
我的目标是绘制此日志文件中的数据,我要绘制的值是date_time格式的sell/buy价格和current_price我从日志文件中读取的行用readlines和每一行,我将date_timecurrent_price附加到date_time_lstcurrent_prices_lst上,然后对于每笔交易,我附加sell/buy价格和date_time到其他列表中,当我使用plt.show获取图形时,获得的值在图形中的顺序也很不明显,而且效率也不高。

如您所见,y轴不按升序或降序排列,这是我做错了,我该如何改进

enter image description here


date_time_lst = []
current_price_lst = [] 
BUYS_prices_lst = [] 
SELLS_prices_lst = [] 
BUYS_DATES =[]
SELLS_DATES = [] 





with open(file__name , "r") as r_handle:
    log_lines = r_handle.readlines()
for line in log_lines:
    line = line.split()
    
    date_time_lst.append(str(line[0] + " " + line[1]))
    current_price_lst.append(line[5])



    if line[4] == "SELL":
            ## the amount of BNB multiplyed by the sell price returns the amount of bitcoin we get from the trade
        amt = float(line[2])*float(line[3])
        SELLS_prices_lst.append(line[3])
        SELLS_DATES.append(str(line[0] + " " + line[1]))
        ## the amount above gets added to the qoute_balance (bitcoin_balance)
        qoute_balance += amt


        ## the original BNB amount is subtracted from the base_balance (binance_balance)
        base_balance -= float(line[2])

    

    elif line[4] == "BUY":
        amt_ = float(line[2])*float(line[3])
        qoute_balance += amt_
        base_balance -= float(line[2])
        BUYS_prices_lst.append(line[3])
        BUYS_DATES.append(str(line[0] + " " + line[1]))



plt.rcParams['axes.facecolor'] = 'grey'
plt.grid(color="black")
plt.scatter(date_time_lst , current_price_lst, color='green' , label='current_coin_prices')
plt.legend(loc='lower right')
plt.show()

1 个答案:

答案 0 :(得分:1)

图形上乱序的值。

  • 数字被读取为字符串。在将它们附加到列表之前,先将它们变成浮点数。

    • price = float(line[3])
      current = float(line[5])
      
  • 将日期和时间列转换为datetime object

    • dt = line[0] + ' ' + line[1]
      dt = datetime.datetime.strptime(dt,'%Y-%m-%d %H:%M:%S')