0

I am using matplotlib and have a data frame df with two columns Date (the dependent variable) and Value (the independent variable). I want to plot Date along the x-axis but currently the dataset are objects, even after converting to numeric. What's the best way to plot these dates? I could either:

  • convert the dates to Day of Year or
  • convert the objects to be some other datatype that matplotlib can plot

Either of these options would be fine.

I looked at the documentation for the datetime package but I really don't understand what's going on. Here's my code:

import matplotlib.pyplot as plt
from datetime import datetime, date,time

fig = plt.figure()
ax = fig.add_subplot(111)
x_points = df['Date']
x_points = x_points.convert_objects(convert_numeric=True) 

>>> x_points
189     11/14/15
191     11/14/15
192     11/14/15
193     11/14/15
194     11/15/15
       ...
2910     2/20/16
2912     2/20/16
2914     2/20/16
2915     2/20/16
2917     2/20/16

and this is what I have for plotting but nothing shows up in my plot

y_points = df['Value']
p = ax.plot(x_points, y_points, 'b')
ax.set_xlabel('x-points')
ax.set_ylabel('y-points')
ax.set_title('Simple XY point plot')
fig.show()

Hoping to get a solution and an explanation to help me learn!

JAG2024
  • 3,987
  • 7
  • 29
  • 58
  • Don't use vanilla Python `datetime` objects... they will alway be *objects*. Try `x_points = pd.to_datetime(x_points)` which should give you a `datetime64` dtype `Series`. – juanpa.arrivillaga Jan 18 '17 at 23:00
  • Yup! That worked. I guess I should delete this question now. – JAG2024 Jan 18 '17 at 23:26
  • Thanks! Please check out my other question: http://stackoverflow.com/questions/42444020/change-frequency-of-x-axis-tick-label-of-datetime-data-in-python-bar-chart-using – JAG2024 Feb 25 '17 at 05:48

0 Answers0