将17位unix时间戳转换为matplotlib可以理解的内容

时间:2017-04-17 20:27:06

标签: json python-3.x numpy matplotlib

下面发布的代码有效&打印出包含一个奇怪的17位数列表的列表列表,该列表是时间戳,例如,20170418030000000

    import matplotlib
    import matplotlib.pyplot as plt
    import matplotlib.dates as mdates
    import matplotlib.ticker as mticker
    from matplotlib.figure import Figure
    from matplotlib import style

    import urllib
    import json
    from urllib.request import urlopen
    import datetime as dt
    import time
    import pandas as pd  # data manipulation
    import numpy as np  # number crunching

    dataLink ='http://api.huobi.com/staticmarket/btc_kline_015_json.js'
    data = urllib.request.urlopen(dataLink)
    data = data.read().decode("utf-8")
    data = json.loads(data)
    data = [data]  
    #ts=data[0][0][0]

    for sublist in data[0]:
        ts=sublist[:1]
        openp=sublist[1:2]
        highp = sublist[2:3]
        lowp = sublist[3:4]
        closep = sublist[4:5]
        vol = sublist[5:6]
        print(ts)
        print(openp)
        print(highp)
        print(lowp)
        print(closep)
        print(vol)
        print()

我的问题是,“如何将时间戳转换为matplotlib允许我沿x轴绘制图形的内容?”我已经删除了大部分的matplotlib内容。在我把它整理出来之后,我会这样做......

提前谢谢

1 个答案:

答案 0 :(得分:0)

考虑使用Pandas模块:

import requests
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib
matplotlib.style.use('ggplot')

dataLink ='http://api.huobi.com/staticmarket/btc_kline_015_json.js'
r = requests.get(dataLink)
df = pd.DataFrame.from_records(r.json())
df[0] = pd.to_datetime(df[0].str[:-3], format='%Y%m%d%H%M%S')
print(df)
df.set_index(0).plot()
plt.show()

结果:

In [37]: print(df)
                      0        1        2        3        4        5
0   2017-04-15 03:00:00  7069.83  7072.40  7040.08  7040.08   2.3042
1   2017-04-15 03:15:00  7069.34  7069.52  7030.48  7069.52   0.5011
2   2017-04-15 03:30:00  7068.65  7068.65  7068.65  7068.65   0.0177
3   2017-04-15 03:45:00  7068.65  7068.65  7068.65  7068.65   0.0000
4   2017-04-15 04:00:00  7065.94  7065.94  7065.94  7065.94   0.0023
5   2017-04-15 04:15:00  7055.60  7055.60  7055.60  7055.60   0.0001
6   2017-04-15 04:30:00  7055.54  7071.63  7030.07  7032.17   3.9101
7   2017-04-15 04:45:00  7040.03  7040.43  7032.20  7040.43   3.2215
8   2017-04-15 05:00:00  7032.48  7067.87  7032.20  7066.97   2.4587
9   2017-04-15 05:15:00  7039.08  7064.18  7038.73  7038.73   0.2955
..                  ...      ...      ...      ...      ...      ...
290 2017-04-18 03:30:00  7088.40  7088.40  7088.40  7088.40   0.0379
291 2017-04-18 03:45:00  7101.01  7102.85  7101.01  7102.85   0.4247
292 2017-04-18 04:00:00  7099.71  7108.00  7089.52  7108.00  15.6556
293 2017-04-18 04:15:00  7108.00  7108.00  7108.00  7108.00   0.0000
294 2017-04-18 04:30:00  7090.58  7090.58  7086.50  7086.50   0.5679
295 2017-04-18 04:45:00  7100.00  7100.00  7086.50  7086.50  10.7334
296 2017-04-18 05:00:00  7098.26  7098.26  7084.61  7084.61   2.1534
297 2017-04-18 05:15:00  7097.70  7097.70  7084.61  7084.61   0.6402
298 2017-04-18 05:30:00  7085.00  7085.00  7085.00  7085.00   0.1674
299 2017-04-18 05:45:00  7085.00  7085.00  7085.00  7085.00   0.0000

[300 rows x 6 columns]

enter image description here