|
From: Mark L. <bre...@ya...> - 2013-04-06 19:08:44
|
On 04/04/2013 19:00, Mark Lawrence wrote: > Sadly no :( I want the day of the month that I'm processing *OR* the > last day. The worst case for this is obviously the 31st of each month. > The rrule code I've given provides exactly that. When transferred to > mpl that doesn't work. Best seen by changing the lines I gave originally to this. start = datetime.date(2013, 4, 5) until = datetime.date(2014, 4, 5) dates = rrule(MONTHLY, bymonthday=(5, -1), bysetpos=1, until=until) rrule output as follows. 2013-04-05 10:15:24 2013-05-05 10:15:24 2013-06-05 10:15:24 2013-07-05 10:15:24 2013-08-05 10:15:24 2013-09-05 10:15:24 2013-10-05 10:15:24 2013-11-05 10:15:24 2013-12-05 10:15:24 2014-01-05 10:15:24 2014-02-05 10:15:24 2014-03-05 10:15:24 Plot attached. > > On 04/04/2013 17:31, Phil Elson wrote: >> Hi Mark, >> >> Thanks for persevering :-) >> >> What is it you want to achieve? Is it that you just want the last day of >> each month as the located value? >> >> Changing your locator to: >> >> ax.xaxis.set_major_locator(MonthLocator(bymonthday = -1)) >> >> Seems to do the trick for me (I've never looked at the mpl date magic, >> so I can give no guarantees). >> >> HTH, >> >> >> On 4 April 2013 17:18, Mark Lawrence >> <bre...@ya... >> <mailto:bre...@ya...>> wrote: >> >> On 01/04/2013 14:48, Mark Lawrence wrote: >> > On 29/03/2013 15:49, Mark Lawrence wrote: >> >> Hi all, >> >> >> >> From http://labix.org/python-dateutil >> >> >> >> "To generate a rrule for the use case of "a date on the >> specified day of >> >> the month, unless it is beyond the end of month, in which case >> it will >> >> be the last day of the month" use the following: >> >> >> >> rrule(MONTHLY, bymonthday=(some_day, -1), bysetpos=1) >> >> >> >> This will generate a value for every calendar month regardless >> of the >> >> day of the month it is started from." >> >> >> >> Using bymonthday with MonthLocator gives ticks on the day given >> and the >> >> last day of the month, which looks extremely ugly. Code below >> demonstrates. >> >> >> >> from dateutil.rrule import * >> >> import datetime >> >> import matplotlib.pyplot as plt >> >> from matplotlib.ticker import FormatStrFormatter, MultipleLocator >> >> from matplotlib.dates import DateFormatter, MonthLocator, DayLocator >> >> >> >> start = datetime.date(2013, 3, 29) >> >> until = datetime.date(2014, 3, 29) >> >> dates = rrule(MONTHLY, bymonthday=(29, -1), bysetpos=1, until=until) >> >> for d in dates:print(d) >> >> >> >> dates = [start, until] >> >> values = [0, 1] >> >> plt.ylabel('Balance') >> >> plt.grid() >> >> ax = plt.subplot(111) >> >> plt.plot_date(dates, values, fmt = 'rx-') >> >> ax.xaxis.set_major_locator(MonthLocator(bymonthday = >> (dates[0].day, -1))) >> >> ax.xaxis.set_major_formatter(DateFormatter('%d/%m/%y')) >> >> ax.yaxis.set_major_formatter(FormatStrFormatter('£%0.2f')) >> >> ax.yaxis.set_minor_locator(MultipleLocator(5)) >> >> plt.axis(xmin=dates[0], xmax=dates[-1]) >> >> plt.setp(plt.gca().get_xticklabels(), rotation = 45, fontsize = 10) >> >> plt.setp(plt.gca().get_yticklabels(), fontsize = 10) >> >> plt.show() >> >> >> > >> > Seems an apt date to realise that I didn't say much :( >> > >> > Assuming that I'm correct would you like an issue raised on the bug >> > tracker? If not please correct the mistake I've made, presumably in >> > reading the docs, which I think are excellent by the way. >> > >> >> Anybody? >> >> -- >> If you're using GoogleCrap™ please read this >> http://wiki.python.org/moin/GoogleGroupsPython. >> > > -- If you're using GoogleCrap™ please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence |