13

I have a python code in which I read a csv file using pandas and store date and time in one column Datetime. Now i want to plot Sensor Value on y-axis and datatime on x-axis. How can i achieve this? My code is below:

import pandas as pd
import datetime
import csv
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
headers = ['Sensor Value','Date','Time']
df = pd.read_csv('C:/Users\Lala Rushan\Downloads\DataLog.CSV',parse_dates=     {"Datetime" : [1,2]},names=headers)
print (df)

Heres some rows from dataset:

                      Datetime  Sensor Value
0     2017/02/17  19:06:17.188             2
1     2017/02/17  19:06:22.360            72
2     2017/02/17  19:06:27.348            72
3     2017/02/17  19:06:32.482            72
4     2017/02/17  19:06:37.515            74
5     2017/02/17  19:06:42.580            70
6     2017/02/17  19:06:47.660            72
chwi
  • 2,752
  • 2
  • 41
  • 66
rushan
  • 211
  • 2
  • 6
  • 16

3 Answers3

27

Make sure your date column is in datetime format and use plot() function in matplotlib. You could do something similar to this. Here x-value will be your date column and y value will be sensor value.

import pandas as pd
from datetime import datetime
import csv
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
headers = ['Sensor Value','Date','Time']
df = pd.read_csv('C:/Users\Lala Rushan\Downloads\DataLog.CSV',names=headers)
print (df)

df['Date'] = df['Date'].map(lambda x: datetime.strptime(str(x), '%Y/%m/%d %H:%M:%S.%f'))
x = df['Date']
y = df['Sensor Value']

# plot
plt.plot(x,y)
# beautify the x-labels
plt.gcf().autofmt_xdate()

plt.show()

enter image description here

Community
  • 1
  • 1
Rohan
  • 5,121
  • 1
  • 17
  • 19
  • when i run the program it gives **ValueError: could not convert string to float: ' 2017/02/17 19:49:27.550'**. how can i solve this? – rushan Feb 21 '17 at 16:37
  • 2
    Your Date column is not in datetime format. You can use before assigning value to x - `df['Date'] = pd.to_datetime(df['Date'], format='%Y/%m/%d %H:%M:%S.%f)' ` – Rohan Feb 21 '17 at 16:47
  • thanks for your comment. now it gives me error: **ValueError: time data ' 2017/02/17 19:06:17.188' doesn't match format specified**. what might be the problem here? the format is identical to what is brought from csv. – rushan Feb 21 '17 at 16:51
  • 1
    Could you post few rows of your dataset in the question? Someone can quickly answer the question. – Rohan Feb 21 '17 at 16:53
  • I have edited the answer. There seems issue with number of spaces between date and time in Date column. Change the lambda expression as per your requirement. I have also added a way to import datetime package. – Rohan Feb 21 '17 at 17:18
  • @rushan Please mark the question as answered if you find it correct – Rohan Feb 21 '17 at 17:49
0

Updated solution for Python 3.9 with date in the format '2022-01-11 23:57' :

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('data.csv')

df['DATE'] = pd.to_datetime(df['DATE'], format='%m/%d/%Y %H:%M')

x = df['DATE']
y = df['Sensor Value']

plt.plot(x,y)
# beautify the x-labels
plt.gcf().autofmt_xdate()

plt.show()
Roy
  • 9
  • 1
0

To get this code to work on the machine I'm currently coding on (MacOS 10.14) with Python 2.7.16, I needed to declare the row of the CSV file that the headers are on. So this is a header=1 in the read_csv section, as is recommended on the official pandas read_csv page here .

My code is below:

import pandas as pd
from datetime import datetime
import csv
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
headers = ['sensor_data','Date']
df = pd.read_csv('output.csv',header=1,names=headers)

df['Date']= pd.to_datetime(df['Date'], format='%Y-%m-%d %H:%M:%S')

df['Date']= df['Date'].map(lambda x: datetime.strptime(str(x), '%Y-%m-%d %H:%M:%S'))

x = df['Date']
print(x)
y = df['sensor_data']

# plot
plt.plot(x,y)
# beautify the x-labels
plt.gcf().autofmt_xdate()

plt.show()
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/30844727) – Register Sole Jan 21 '22 at 03:38