Skip to content Skip to sidebar Skip to footer

Python: Plot Dataframe Of Datetime Entries

My data looks like this : 900324492 900405679 900472531 1 2017-04-03 08:04:09 2017-04-03 07:49:53 2017-04-03 07:52:39 2 2017-04-03 08:05:36 2017-04-03 07:54:36 2017-04

Solution 1:

Please check my example from yours. You should focus on to_pydatetime() and date2num() and np.nan. (You have to tag y axis to datetime format finally.)

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as dates


df = pd.read_csv('test.csv', header=None)
df2 = df.apply(lambda x : pd.to_datetime(x))

fig = plt.figure()                                                                                                                                                                                                                                                             
ax = fig.add_subplot(111)                                                                                                                                                                                                                                                      
y = df2.ix[:, 1]                                                                                                                                                                                                                                         
x = df2.index.values

deffix(x):
    try:
        return dates.date2num(x.to_pydatetime())
    except:
        return np.nan

y_lab = [str(e) for e in y]
y_ = [fix(e) for e in y]

ax.scatter(x=x, y=y_)

plt.yticks(y_, y_lab, rotation=0, size=6)
plt.show()

enter image description here

Post a Comment for "Python: Plot Dataframe Of Datetime Entries"