简体   繁体   English

Python:绘制时间戳数据框matplotlib

[英]Python: plot timestamp data frame matplotlib

I am plotting values over the time (ie hourly) I have a dataframe dfout as following 我正在绘制一段时间内的值(即每小时)我有一个数据帧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

and I want to plot the column val as following 我想绘制列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)

在此输入图像描述

However I would like the xlabel between 08:00:00 to 16:00:00 hour by hour, so: 08:00:00 , 09:00:00 , 10:00:00 ,...., 16:00:00 . 不过,我想的xlabel 08:00:0016:00:00小时按小时,所以: 08:00:0009:00:0010:00:00 ,..., 16:00:00

Simply choose the position of your ticks using ax.set_xticks() and feed it a custom list of datetime objects created for that purpose. 只需使用ax.set_xticks()选择刻度线的位置,并为其提供为此目的创建的自定义datetime对象列表。 The issue is that the label representations of those object are floats. 问题是这些对象的标签表示是浮点数。 You can remedy this by creating another list that contains the time-strings as you want them to look like, which I called ticklabels in this solution. 你可以通过创建另一个包含你想要的时间字符串的列表来解决这个问题,我在这个解决方案中称之为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()

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM