def monthly_labels(t1, t2, every=6):
"""Returns labels for the months between two dates.
The first label corresponds to the starting month, while the last label
corresponds to the month after the ending month. This is done for better
bracketing of the plot. By default, only the label every 6 months is
non-empty.
Parameters
----------
t1 : datetime object
Starting date
t2 : datetime object
End date
every : int, default is 6
Returns
-------
list
"""
# Start from first day of month, for bracketing
labels = []
if t2.year > t1.year:
# append months until the end of the year
for month in range(t1.month, 13):
labels.append(calendar.month_abbr[month][:3])
for year in range(t1.year + 1, t2.year):
for month in range(1, 13):
labels.append(calendar.month_abbr[month][:3])
for month in range(1, (t2.month % 12) + 1):
labels.append(calendar.month_abbr[month][:3])
else:
for month in range(t1.month, (t2.month % 12) + 1):
labels.append(calendar.month_abbr[month][:3])
# append next month for bracketing
labels.append(calendar.month_abbr[(t2.month % 12) + 1][:3])
labels = [labels[i]
if i % every == 0
else ''
for i in range(len(labels))]
return labels
评论列表
文章目录