29

I have a fixed-width data file containing dates, but when I try to plot the data the dates are not displayed properly on the x-axis.

My files looks like

2014-07-10 11:49:14.377102    45
2014-07-10 11:50:14.449150    45
2014-07-10 11:51:14.521168    21
2014-07-10 11:52:14.574241     8
2014-07-10 11:53:14.646137    11
2014-07-10 11:54:14.717688    14

etc

and I use pandas to read in the file

#! /usr/bin/env python
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_fwf('myfile.log',header=None,names=['time','amount'],widths=[27,5])
data.time = pd.to_datetime(data['time'], format='%Y-%m-%d %H:%M:%S.%f')
plt.plot(data.time,data.amount)
plt.show()

So I suppose the issue here is conversion from pandas to matplotlib datetime, How would one do a conversion?

I also tried with pandas directly:

data.time = pd.to_datetime(data['time'], format='%Y-%m-%d %H:%M:%S.%f')
data.set_index('time') # Fails!!
data.time.plot()

but this fails with

TypeError: Empty 'Series': no numeric data to plot

2 Answers 2

41

If you use a list containing the column name(s) instead of a string, data.set_index will work

The following should show the dates on x-axis:

#! /usr/bin/env python
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_fwf('myfile.log',header=None,names=['time','amount'],widths=[27,5])
data.time = pd.to_datetime(data['time'], format='%Y-%m-%d %H:%M:%S.%f')
data.set_index(['time'],inplace=True)
data.plot()

#OR 
plt.plot(data.index, data.amount)
Sign up to request clarification or add additional context in comments.

Comments

7

The below code would plot the dates and stock price of particular stock. I wanted date on x axis and price data of stock on y axis. The below code worked for me.

load the csv into a pandas data frame and just load the requried column of csv file into an array and plot it using plt(column_on_x_axis, column_in_y_axis, linestyle) Ofcourse, matplotlib.pyplot should be included.

data = pd.readcsv(your_file.csv)
date_array  = data['Date']
price_array = data['Prices']
plt.plot(date_array, price_array, linestyle = 'solid')

1 Comment

It is missing the required imports: import matplotlib.pyplot as plt import pandas as pd

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.