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!