Hi John,
I've been making spacecraft trajectory "Pork Chop Plots", a contour plot of a z-value (such amount of fuel required) over a range of Earth departure date x-values and Mars arrival date y-values.
Since I needed dates on both x and y axes, it was useful for me to factor out the date locator/formatter selection code from plot_date() into two Axes methods: xaxis_date() and yaxis_date().
The plot_date() implementation then becomes just a call to plot() followed by a call to xaxis_date().
My current implementation is below. Is this general-purpose enough for me to submit? If so, I'll make up some diff files and mail them in.
Thanks,
Michael
···
#===========================================================================
def xaxis_date(self, tz=None):
"""Choose date ticks and labels for the x-axis.
= INPUT VARIABLES
- tz The timezone to use in plotting the dates.
Defaults to rc value.
"""
span = self.dataLim.intervalx().span()
locator, formatter = self.chooseDateFmt( span, tz )
self.xaxis.set_major_locator(locator)
self.xaxis.set_major_formatter(formatter)
#===========================================================================
def yaxis_date(self, tz=None):
"""Choose date ticks and labels for the y-axis.
= INPUT VARIABLES
- tz The timezone to use in plotting the dates.
Defaults to rc value.
"""
span = self.dataLim.intervaly().span()
locator, formatter = self.chooseDateFmt( span, tz )
self.yaxis.set_major_locator(locator)
self.yaxis.set_major_formatter(formatter)
#===========================================================================
def chooseDateFmt(self, span, tz=None):
"""Chooses an appropriate date formatter and tick locator for a span.
= INPUT VARIABLES
- span The span of the plot data.
- tz The timezone to use in plotting the dates.
Defaults to rc value.
= RETURN VALUE
- A tuple of ( locator, formatter ) for the input span.
"""
# The code below was copied from Axes.plot_date().
if span==0:
span = SEC_PER_HOUR
minutes = span*24*60
hours = span*24
days = span
weeks = span/7.
months = span/31. # approx
years = span/365.
numticks = 5
if years>numticks:
locator = YearLocator(int(years/numticks), tz=tz) # define
fmt = '%Y'
elif months>numticks:
locator = MonthLocator(tz=tz)
fmt = '%b %Y'
elif weeks>numticks:
locator = WeekdayLocator(tz=tz)
fmt = '%a, %b %d'
elif days>numticks:
locator = DayLocator(interval=math.ceil(days/numticks), tz=tz)
fmt = '%b %d'
elif hours>numticks:
locator = HourLocator(interval=math.ceil(hours/numticks), tz=tz)
fmt = '%H:%M\n%b %d'
elif minutes>numticks:
locator = MinuteLocator(interval=math.ceil(minutes/numticks), tz=tz)
fmt = '%H:%M:%S'
else:
locator = MinuteLocator(tz=tz)
fmt = '%H:%M:%S'
formatter = DateFormatter(fmt, tz=tz)
return locator, formatter