Plot pandas dates in matplotlib

后端 未结 1 1248
春和景丽
春和景丽 2020-12-03 07:18

I have a fixed-width data file containing dates, but when I try to plot the data the dates are not displayed properly on the x-axis.

My files looks like



        
相关标签:
1条回答
  • 2020-12-03 07:34

    If you use a list containing the column name(s) instead of a string, data.set_index will work

    The following should show the dates on x-axis:

    #! /usr/bin/env python
    import pandas as pd
    import matplotlib.pyplot as plt
    data = pd.read_fwf('myfile.log',header=None,names=['time','amount'],widths=[27,5])
    data.time = pd.to_datetime(data['time'], format='%Y-%m-%d %H:%M:%S.%f')
    data.set_index(['time'],inplace=True)
    data.plot()
    
    #OR 
    plt.plot(data.index, data.amount)
    
    0 讨论(0)
提交回复
热议问题