2015-04-03 114 views
0

我有日期时间值包括微秒的熊猫数据帧:大熊猫/ matplotlib日期时间刻度标记

      column1 column2 
time                   
1900-01-01 10:39:52.887916 19363.876 19362.7575 
1900-01-01 10:39:53.257916 19363.876 19362.7575 
1900-01-01 10:39:53.808007 19363.876 19362.7575 
1900-01-01 10:39:53.827894 19363.876 19362.7575 
1900-01-01 10:39:54.277931 19363.876 19362.7575 

我绘制数据帧如下:图像低于该微秒上

def plot(df): 
    ax = df.plot(y='column1', figsize=(20, 8)) 
    df.plot(y='column2', ax=ax) 

    ax.get_yaxis().get_major_formatter().set_useOffset(False) 

    mpl.pyplot.show() 

通知显示为%f而非其实际值。

也就是说,而非10:39:52.887916它显示10:39:52.%f

我怎样才能在刻度标记(即使是只有几显著位)显示实际微秒?

enter image description here

+0

您是否在某处设置了DateFormatter?这看起来像一个传递给DateFormatter的字符串格式中的一个leetle typo。有些东西不是默认的;当我运行你的代码时,我得到了一个完全不同的xticklabels错误集合。可能是熊猫,可能是海豹? – cphlewis 2015-04-03 04:36:35

+0

我没有设置它,也许它是熊猫。我没有使用seaborn – 2015-04-03 04:55:15

回答

2

您应该能够在主刻度设置为你想要的格式,使用set_major_formatter

In [14]: 

import matplotlib as mpl 
import matplotlib.dates 
df = pd.DataFrame({'column1': [1,2,3,4], 
        'column2': [2,3,4,5]}, 
        index =pd.to_datetime([1e8,2e8,3e8,4e8])) 
def plot(df): 
    ax = df.plot(y='column1', figsize=(20, 8)) 
    df.plot(y='column2', ax=ax) 

    ax.get_yaxis().get_major_formatter().set_useOffset(False) 
    ax.get_xaxis().set_major_formatter(matplotlib.dates.DateFormatter('%H:%M:%S.%f')) 

    #mpl.pyplot.show() 
    return ax 

print df 
          column1 column2 
1970-01-01 00:00:00.100000  1  2 
1970-01-01 00:00:00.200000  2  3 
1970-01-01 00:00:00.300000  3  4 
1970-01-01 00:00:00.400000  4  5 

enter image description here

如果问题不消失,那么我认为某处在代码中格式化程序格式被错误地指定,即%%f而不是%f,它返回一个文字'%'字符。

+0

嗯,不知道我的代码中发生了什么。我抄录了您的摘录,并且正确打印了微秒。在我的代码中,我完成了与你完全相同的工作,但是按照我所描述的那样获取'%f'!很奇怪! – 2015-04-10 01:31:52

+0

我用'df = my_df'替换了你的'df = pd.Dataframe'调用,并将micros打印为'%.f',所以这与数据帧有关 – 2015-04-10 01:37:33