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.
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)
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With