Reputation: 345
I am new to Python and am trying to create a simple line graph with time on the x axis and values on the y. I have a CSV file from which to import the data.
A sample of the data looks like this
I have tried to use the following code:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
a_data = pd.read_csv("Data Sampler.csv")
plt.plot(a_data.Timestamp, a_data.a)
However, I am receiving an error message: AttributeError: 'Series' object has no attribute 'find'
a_data.dtypes tells me that Timestamp is an object and A is float 64.
Can anyone help? From googling a solution, it looks like I need to convert the timestamp to a datetime within Python - however I am not having much luck with this (lots of error messages).
Thanks in advance.
Upvotes: 2
Views: 6613
Reputation: 18561
If you're using matplotlib.__version__ >= 1.5.0
, then the following should work:
In [4]: df
Out[4]:
Timestamp A
0 03/10/2016 16:00 0.033361
1 04/10/2016 16:01 0.123108
2 05/10/2016 16:02 0.021805
In [5]: df.index = pd.to_datetime(df.Timestamp, dayfirst=True)
In [6]: df
Out[6]:
Timestamp A
Timestamp
2016-10-03 16:00:00 03/10/2016 16:00 0.033361
2016-10-04 16:01:00 04/10/2016 16:01 0.123108
2016-10-05 16:02:00 05/10/2016 16:02 0.021805
In [7]: plt.plot('A', data=df)
Out[7]: [<matplotlib.lines.Line2D at 0x7f8e3c0e8150>]
In [7]: plt.show()
Depending on the scale of your data, you can adjust the tick formatting of the x-axis using the appropriate tick formatters, see here for an example.
Upvotes: 3