Python:绘制时间戳数据框matplotlib

时间:2015-10-15 20:05:26

标签: python datetime matplotlib dataframe

我在一段时间内绘制价值(即每小时) 我有一个数据框dfout,如下所示

    val     x            y                      ID                              time
0   6.0 48.730304   11.594837   0efd2316-feb9-461c-b102-045aef8e22fe    2014-03-10 08:02:02
1   6.5 48.731639   11.602004   0efd2316-feb9-461c-b102-045aef8e22fe    2014-03-10 08:02:26
2   7.0 48.740104   11.641433   0efd2316-feb9-461c-b102-045aef8e22fe    2014-03-10 08:04:45
3   6.5 48.744026   11.648048   0efd2316-feb9-461c-b102-045aef8e22fe    2014-03-10 08:05:11
4   6.0 48.747356   11.654539   0efd2316-feb9-461c-b102-045aef8e22fe    2014-03-10 08:05:32
5   5.5 48.749050   11.660844   0efd2316-feb9-461c-b102-045aef8e22fe    2014-03-10 08:05:50
6   5.0 48.749935   11.666812   0efd2316-feb9-461c-b102-045aef8e22fe    2014-03-10 08:06:10
7   4.5 48.751007   11.677590   0efd2316-feb9-461c-b102-045aef8e22fe    2014-03-10 08:06:54
8   4.0 48.742317   11.675558   0efd2316-feb9-461c-b102-045aef8e22fe    2014-03-10 08:08:31
9   4.5 48.736461   11.675782   0efd2316-feb9-461c-b102-045aef8e22fe    2014-03-10 08:08:55
10  5.0 48.729659   11.675481   0efd2316-feb9-461c-b102-045aef8e22fe    2014-03-10 08:09:20
11  5.5 48.725871   11.673145   0efd2316-feb9-461c-b102-045aef8e22fe    2014-03-10 08:09:35

我希望将列val绘制成如下

import matplotlib.dates as md
fig=figure(figsize=(20,10))
ax=plt.gca()
xfmt = md.DateFormatter('%H:%M:%S')
plt.xticks( rotation=25 )
ax.xaxis.set_major_formatter(xfmt)
plt.title("Temperature")
plt.ylabel(r'Temperature, $^{o}C$')
ax.plot(datenumes,dfout.val, marker='.',lw=0)

enter image description here

但是我希望08:00:0016:00:00小时之间的xlabel,所以:08:00:0009:00:0010:00:00,...., 16:00:00

1 个答案:

答案 0 :(得分:3)

只需使用ax.set_xticks()选择刻度线的位置,然后为其提供为此目的创建的datetime个对象的自定义列表。 问题是这些对象的标签表示是浮点数。您可以通过创建另一个包含您希望它们的时间字符串的列表来解决这个问题,我在此解决方案中调用了ticklabels

import matplotlib.dates as md
import matplotlib.pyplot as plt
from  datetime import *

val = [6, 6.5, 7, 6.5, 6, 5.5, 5, 4.5, 4, 4.5, 5, 5.5]
datenumes = [datetime(2014, 3, 10, 8, 2, 2), 
    datetime(2014, 3, 10, 8, 2, 26), 
    datetime(2014, 3, 10, 10, 4, 45), 
    datetime(2014, 3, 10, 10, 5, 11), 
    datetime(2014, 3, 10, 12, 5, 32),
    datetime(2014, 3, 10, 12, 5, 50), 
    datetime(2014, 3, 10, 15, 6, 10), 
    datetime(2014, 3, 10, 16, 6, 54), 
    datetime(2014, 3, 10, 17, 8, 31), 
    datetime(2014, 3, 10, 18, 8, 55), 
    datetime(2014, 3, 10, 19, 9, 20), 
    datetime(2014, 3, 10, 22, 9, 35)]

# creates list of datetimes where ticks should be set
ticks = [datetime(2014, 3, 10, 8, 0, 0), 
        datetime(2014, 3, 10, 10, 0, 0),
        datetime(2014, 3, 10, 12, 0, 0),
        datetime(2014, 3, 10, 14, 0, 0),
        datetime(2014, 3, 10, 16, 0, 0),
        datetime(2014, 3, 10, 18, 0, 0),
        datetime(2014, 3, 10, 20, 0, 0),
        datetime(2014, 3, 10, 22, 0, 0) ]

# generate labels for the datetimes used as ticks
ticklabels = [str(tick)[11:] for tick in ticks]

#  PLOT
fig=plt.figure(figsize=(20,10))
ax=plt.gca()
plt.xticks( rotation=25 )

# set the ticks according to the list tick
ax.set_xticks(ticks)
##  set labels to human readable format
ax.set_xticklabels(ticklabels)

plt.title("Temperature")
plt.ylabel(r'Temperature, $^{o}C$')
ax.plot(datenumes, val, marker='.', lw=0)
plt.show()

enter image description here