1

I have written a python program to get data from csv using pandas and plot the data using matplotlib. My code is below with result:

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

#f = plt.figure(figsize=(10, 10))
df.plot(x='Datetime',y='Sensor Value',) # figure.gca means "get current axis"
plt.title('Title here!', color='black')
plt.tight_layout()
plt._show()

enter image description here

Now as you can see the x-axis looks horrible. How can I plot the x-axis for a single date and time interval so that it does not looks like overlapping each other? I have stored both date and time as one column in my dataframe.

My Dataframe looks like this:

                       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

1 Answer 1

4

Hacky way

Try this:

import pylab as pl
pl.xticks(rotation = 90)

It will rotate the labels by 90 degrees, thus eliminating overlap.

Cleaner way

Check out this link which describes how to use fig.autofmt_xdate() and let matplotlib pick the best way to format your dates.

Pandas way

Use to_datetime() and set_index with DataFrame.plot():

df.Datetime=pd.to_datetime(df.Datetime)
df.set_index('Datetime')
df['Sensor Value'].plot()

pandas will then take care to plot it nicely for you:

enter image description here

my Dataframe looks like this:

                      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
Sign up to request clarification or add additional context in comments.

10 Comments

i am trying the pandas way. It is giving error on 'Use' that 'Python 3.5 does not support backquotes'. Whats the solution for this?
its giving error now that 'unresolved reference to Use' and also same error for to_datetime() ,set_index and DataFrame.plot().
sorry for that. Now, it gives the error: Unrecognized value type: <class 'str'>. I think I am missing some basic thing here as i am really a newbie in python.
I can only help you if you give me a sample of your dataframe
I have added the sample of dataframe in the original question.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.