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!

3
  • 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. Commented Jan 18, 2017 at 23:00
  • Yup! That worked. I guess I should delete this question now. Commented Jan 18, 2017 at 23:26
  • Thanks! Please check out my other question: stackoverflow.com/questions/42444020/… Commented Feb 25, 2017 at 5:48

0

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.