I have a conversion of timestamps to DateTime
objects as below:
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)
As can be seen from this example, the plotting is done using the whole time object, including the information down to nanosecond level. This makes the xlables hard to read. Is there a way to 'clean' the x lables with an option in the plotting or already in the conversion? I would like the timestamp to appear in the format HH:MM:SS.
Any help is highly appreciated!
Use a matplotlib.dates.DateFormatter
to specify the date format:
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()