Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot pandas dates in matplotlib

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

like image 928
Gerhard Avatar asked Aug 21 '14 01:08

Gerhard


People also ask

Can Matplotlib plot dates?

Luckily, matplotlib provides functionality to change the format of a date on a plot axis using the DateFormatter module, so that you can customize the look of your labels without having to rotate them.


Video Answer


2 Answers

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) 
like image 75
Osmond Bishop Avatar answered Sep 24 '22 17:09

Osmond Bishop


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') 
like image 37
Sai Siril Avatar answered Sep 22 '22 17:09

Sai Siril