xaxis上的纳秒时间戳

时间:2016-06-23 08:48:18

标签: datetime pandas matplotlib dataframe axis-labels

我将时间戳转换为DateTime个对象,如下所示:

import pandas as pd

s1 = {'Timestamp':['20160208_095900.51','20160208_095901.51','20160208_095902.51','20160208_095903.51',
                 '20160208_095904.51','20160208_095905.51','20160208_095906.51','20160208_095907.51',
                 '20160208_095908.51','20160208_095909.51'],
      'Data' : [2300,2500,2600,2700,2800,2900,3000,3100,3200,3300]}
df = pd.DataFrame(s1)

df['Date'] = pd.to_datetime(df['Timestamp'], format = '%Y%m%d_%H%M%S.%f')

print df

fig = plt.figure(figsize=(8,6))
plt.plot(df.Date, df.Data)

从这个例子中可以看出,绘图是使用整个时间对象完成的,包括低至纳秒级别的信息。这使得xlables难以阅读。 有没有办法在绘图中或已经在转换中使用选项“清理”x标签? 我希望时间戳以 HH:MM:SS 格式显示。

非常感谢任何帮助!

3 个答案:

答案 0 :(得分:2)

使用matplotlib.dates.DateFormatter指定日期格式:

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

s1 = {'Timestamp':['20160208_095900.51','20160208_095901.51','20160208_095902.51','20160208_095903.51',
                 '20160208_095904.51','20160208_095905.51','20160208_095906.51','20160208_095907.51',
                 '20160208_095908.51','20160208_095909.51'],
      'Data' : [2300,2500,2600,2700,2800,2900,3000,3100,3200,3300]}
df = pd.DataFrame(s1)
df['Date'] = pd.to_datetime(df['Timestamp'], format = '%Y%m%d_%H%M%S.%f')

fig, ax = plt.subplots(figsize=(8,6))
xfmt = mdates.DateFormatter('%H:%M:%S')
ax.xaxis.set_major_formatter(xfmt)
# automatically rotates the tick labels
fig.autofmt_xdate()

ax.plot(df['Date'], df['Data'])
plt.show()

enter image description here

答案 1 :(得分:1)

一种可能的解决方案是使用dt.strftime自定义格式创建新列:

df['Date1'] = df['Date'].dt.strftime('%H:%M:%S') 
print (df)
   Data           Timestamp                    Date     Date1
0  2300  20160208_095900.51 2016-02-08 09:59:00.510  09:59:00
1  2500  20160208_095901.51 2016-02-08 09:59:01.510  09:59:01
2  2600  20160208_095902.51 2016-02-08 09:59:02.510  09:59:02
3  2700  20160208_095903.51 2016-02-08 09:59:03.510  09:59:03
4  2800  20160208_095904.51 2016-02-08 09:59:04.510  09:59:04
5  2900  20160208_095905.51 2016-02-08 09:59:05.510  09:59:05
6  3000  20160208_095906.51 2016-02-08 09:59:06.510  09:59:06
7  3100  20160208_095907.51 2016-02-08 09:59:07.510  09:59:07
8  3200  20160208_095908.51 2016-02-08 09:59:08.510  09:59:08
9  3300  20160208_095909.51 2016-02-08 09:59:09.510  09:59:09

fig = plt.figure(figsize=(8,6))
df.plot(x='Date1', y='Data', rot=40)
plt.show()

graph

如果索引不重要,您可以按自定义datetime设置新索引,然后使用Series.plot

df.index = df['Date'].dt.strftime('%H:%M:%S') 
print (df)
          Data           Timestamp                    Date
Date                                                      
09:59:00  2300  20160208_095900.51 2016-02-08 09:59:00.510
09:59:01  2500  20160208_095901.51 2016-02-08 09:59:01.510
09:59:02  2600  20160208_095902.51 2016-02-08 09:59:02.510
09:59:03  2700  20160208_095903.51 2016-02-08 09:59:03.510
09:59:04  2800  20160208_095904.51 2016-02-08 09:59:04.510
09:59:05  2900  20160208_095905.51 2016-02-08 09:59:05.510
09:59:06  3000  20160208_095906.51 2016-02-08 09:59:06.510
09:59:07  3100  20160208_095907.51 2016-02-08 09:59:07.510
09:59:08  3200  20160208_095908.51 2016-02-08 09:59:08.510
09:59:09  3300  20160208_095909.51 2016-02-08 09:59:09.510

fig = plt.figure(figsize=(8,6))
df.Data.plot(rot=40)
plt.show()

答案 2 :(得分:1)

这种懒惰的做法怎么样?

In [11]: fig, ax = plt.subplots(figsize=(8,6))

In [12]: df.set_index(df.Date.dt.time).plot(ax=ax, rot=40)
Out[12]: <matplotlib.axes._subplots.AxesSubplot at 0x99de438>

enter image description here