Reputation: 5266
I have a list of numbers Z
, a list Z_means
(which is a list of means of the numbers in Z using a moving average, and it has not the same len as Z), and another list P
of the same len as Z_means.
I have the following two plots:
pylab.figure(00);
pylab.plot(range(len(Z)), Z, 'go', [i for i in range(len(Z_means))], Z_means)
pylab.figure(11);
pylab.plot( [i for i in range(len(P))], P)
I have a list of dates D = [d0, d1, d2, ...]
each date is of type datetime.datetime.
How can I do these plots using those dates instead of the i values (on the x axis) ? The i'th value correspond to the i'th date in D.
Note that I want just the month and year to appear on the plot (not the whole date)
Upvotes: 2
Views: 1742
Reputation: 949
I suggest using the matplotlib plot_date method to plot the data along with the matplotlib.dates.DateFormatter to put only the month and year on the x-axis.
Here is a (pretty minimal) working example:
import datetime
import matplotlib.pyplot as plt
from matplotlib.dates import DayLocator, HourLocator, DateFormatter, drange
import numpy as np
date1 = datetime.datetime( 2000, 3, 2)
date2 = datetime.datetime( 2001, 10, 10)
delta = datetime.timedelta(days=28)
dates = drange(date1, date2, delta)
ys = np.arange( len(dates) )
fig, ax = plt.subplots()
ax.plot_date(dates, ys*ys)
ax.xaxis.set_major_formatter( DateFormatter('%Y-%m') )
plt.show()
Upvotes: 1