Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot dates vs values in Python

I have a very standard dataset with 2 columns, 1 for dates and 1 for values. I've put them into two arrays:

dates
['1/1/2014', '1/2/2014', '1/3/2014', ...]

values
[1423, 4321, 1234, ...]

How can I create a simple line graph with "values" on the y-axis and "dates" on the x-axis?

What I've tried:

I can do a "Hello world" line plot with only "values" in just 1 line (awesome):

import numpy as np
import matplotlib.pyplot as plt

plt.plot(values)

Next, let's add "dates" as the x-axis. At this point I'm stuck. How can I transform my "dates" array, which is strings, into an array that is plottable?

Looking at examples, I believe we are supposed to cast the strings into Python Date objects. Let's import those libraries:

import datetime
import matplotlib.dates

Ok so now I can transform a string into a date time.strptime(dates[1], '%m/%d/%Y'). How can I transform the entire array? I could write a loop, but assuming there is a better way.

I'm not 100% sure I'm even on the right path to making something usable for "Dates" vs "values". If you know the code to make this graph (I'm assuming it's very basic once you know Python + the libraries), please let me know.

like image 984
Don P Avatar asked Jul 22 '14 05:07

Don P


2 Answers

Ok, knew it was a 1 liner. Here is how to do it:

So you start with your value and dates (as strings) array:

dates = ['1/1/2014', '1/2/2014', '1/3/2014', ...]

values = [1423, 4321, 1234, ...]

Then turn your dates which are strings into date objects:

date_objects = [datetime.strptime(date, '%m/%d/%Y').date() for date in dates]

Then just plot.

plt.plot(date_objects, values)
like image 174
Don P Avatar answered Oct 13 '22 03:10

Don P


Remember you can use xticks and use whatever the hell you want in place of the values. So, you can do a

xVals = range(len(values))
plot(xVals, values)
xticks(xVals, dates)

This is ok if your dates are linear like you have. Otherwise, you can always get a number which quantifies the dates (number of days from the first day (see the datetime module) and use that for xVals.

like image 36
ssm Avatar answered Oct 13 '22 02:10

ssm