如何将显示绘图数据秒数的X轴添加到matplotlib pyplot价格量图?

时间:2018-08-15 18:50:56

标签: python-3.x pandas matplotlib

以下代码使用制表符分隔的csv文件中的数据绘制价格图表。每行包含这些列的值:IDX,TRD,TIMESTAMPMS,VOLUME和PRICE。照原样,X轴显示IDX值。我希望X轴显示从时间戳算出的秒数(以毫秒为单位),该秒数附于每行。如何获得?

import matplotlib.pyplot as plt

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

data = pd.read_csv('secondary-2018-08-12-21-32-56.csv', index_col=0, sep='\t')
print(data.head(50))
fig, ax = plt.subplots(nrows=2, sharex=True, figsize=(10,5))

ax[0].plot(data.index, data['PRICE'])
ax[1].bar(data.index, data['VOLUME'])

plt.show()

绘制的图形如下:

Price / volume graph on 50 data rows

以下是

显示的数据
print(data.head(50))

说明:

     TRD    TIMESTAMPMS    VOLUME    PRICE
IDX                                       
1      4  1534102380000  0.363583  6330.41
2     20  1534102381000  5.509219  6329.13
3      3  1534102382000  0.199049  6328.69
4      5  1534102383000  1.055055  6327.36
5      2  1534102384000  0.006343  6328.26
6      4  1534102385000  0.167502  6330.38
7      1  1534102386000  0.002039  6326.69
8      0  1534102387000  0.000000  6326.69
9      4  1534102388000  0.163813  6327.62
10     2  1534102389000  0.007060  6326.66
11     4  1534102390000  0.015489  6327.64
12     5  1534102391000  0.035618  6328.35
13     2  1534102392000  0.006003  6330.12
14     5  1534102393000  0.172913  6328.77
15     1  1534102394000  0.019972  6328.03
16     3  1534102395000  0.007429  6328.03
17     1  1534102396000  0.000181  6328.03
18     3  1534102397000  1.041483  6328.03
19     2  1534102398000  0.992897  6328.74
20     3  1534102399000  0.061871  6328.11
21     2  1534102400000  0.000123  6328.77
22     4  1534102401000  0.028650  6330.25
23     2  1534102402000  0.035504  6330.01
24     3  1534102403000  0.982527  6330.11
25     5  1534102404000  0.298366  6329.11
26     2  1534102405000  0.071119  6330.06
27     3  1534102406000  0.025547  6330.02
28     2  1534102407000  0.003413  6330.11
29     4  1534102408000  0.431217  6330.05
30     3  1534102409000  0.021627  6330.23
31     1  1534102410000  0.009661  6330.28
32     1  1534102411000  0.004209  6330.27
33     1  1534102412000  0.000603  6328.07
34     6  1534102413000  0.655872  6330.31
35     1  1534102414000  0.000452  6328.09
36     7  1534102415000  0.277340  6328.07
37     8  1534102416000  0.768351  6328.04
38     1  1534102417000  0.078893  6328.20
39     2  1534102418000  0.000446  6326.24
40     2  1534102419000  0.317381  6326.83
41     2  1534102420000  0.100009  6326.24
42     2  1534102421000  0.000298  6326.25
43     6  1534102422000  0.566820  6330.00
44     1  1534102423000  0.000060  6326.30
45     2  1534102424000  0.047524  6326.30
46     4  1534102425000  0.748773  6326.61
47     3  1534102426000  0.007656  6330.23
48     1  1534102427000  0.000019  6326.32
49     1  1534102428000  0.000014  6326.34
50     0  1534102429000  0.000000  6326.34

2 个答案:

答案 0 :(得分:0)

我相信您需要data.setindex('TIMESTAMPMS')才能使轴自动缩放

答案 1 :(得分:0)

我不知道我是否正确理解了您,请尝试:

data['TIMESTAMPMS'] = data['TIMESTAMPMS']/1000
ax[0].plot(data['TIMESTAMPMS'], data['PRICE'])
ax[1].bar(data['TIMESTAMPMS'], data['VOLUME'])