Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e927e25

Browse files
committed
added autofmt for date plots
svn path=/trunk/matplotlib/; revision=3239
1 parent 6d4eddb commit e927e25

5 files changed

Lines changed: 56 additions & 5 deletions

File tree

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2007-04-16 Added autofmt_xdate to handle adjusting the bottom and
2+
rotating the tick labels for date plots when the ticks
3+
often overlap
4+
15
2007-04-09 Beginnings of usetex support for pdf backend. -JKS
26

37
2007-04-07 Fixed legend/LineCollection bug. Added label support

examples/load_converter.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
from pylab import figure, show, datestr2num, load
1+
from matplotlib.dates import strpdate2num
2+
from matplotlib.mlab import load
3+
from pylab import figure, show
4+
25
dates, closes = load(
36
'data/msft.csv', delimiter=',',
4-
converters={0:datestr2num}, skiprows=1, usecols=(0,2),
5-
unpack=True)
7+
converters={0:strpdate2num('%d-%b-%y')},
8+
skiprows=1, usecols=(0,2), unpack=True)
69

710
fig = figure()
811
ax = fig.add_subplot(111)
9-
ax.plot_date(dates, closes)
12+
ax.plot_date(dates, closes, '-')
13+
fig.autofmt_xdate()
1014
show()

lib/matplotlib/dates.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,22 @@ def _from_ordinalf(x, tz=None):
167167

168168
return dt
169169

170+
class strpdate2num:
171+
"""
172+
Use this class to parse date strings to matplotlib datenums when
173+
you know the date format string of the date you are parsing. See
174+
examples/load_demo.py
175+
"""
176+
def __init__(self, fmt):
177+
""" fmt: any valid strptime format is supported """
178+
self.fmt = fmt
179+
180+
def __call__(self, s):
181+
"""s : string to be converted
182+
return value: a date2num float
183+
"""
184+
return date2num(datetime.datetime(*time.strptime(s, self.fmt)[:6]))
185+
170186
def datestr2num(d):
171187
"""
172188
Convert a date string to a datenum using dateutil.parser.parse

lib/matplotlib/figure.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,33 @@ def __init__(self,
158158

159159
self._cachedRenderer = None
160160

161+
def autofmt_xdate(self, bottom=0.2, rotation=30, ha='right'):
162+
"""
163+
A common use case is a number of subplots with shared xaxes
164+
where the x-axis is date data. The ticklabels are often
165+
long,and it helps to rotate them on the bottom subplot and
166+
turn them off on other subplots. This function will raise a
167+
RuntimeError if any of the Axes are not Subplots.
168+
169+
bottom : the bottom of the subplots for subplots_adjust
170+
rotation: the rotation of the xtick labels
171+
ha : the horizontal alignment of the xticklabels
172+
173+
174+
"""
175+
176+
for ax in self.get_axes():
177+
if not hasattr(ax, 'is_last_row'):
178+
raise RuntimeError('Axes must be subplot instances; found %s'%type(ax))
179+
if ax.is_last_row():
180+
for label in ax.get_xticklabels():
181+
label.set_ha(ha)
182+
label.set_rotation(rotation)
183+
else:
184+
for label in ax.get_xticklabels():
185+
label.set_visible(False)
186+
self.subplots_adjust(bottom=bottom)
187+
161188
def get_children(self):
162189
'get a list of artists contained in the figure'
163190
children = [self.figurePatch]

lib/matplotlib/pylab.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@
236236
# eg a bad pytz install. I don't want to break all of matplotlib for
237237
# date support
238238
try:
239-
from dates import date2num, num2date, datestr2num, drange,\
239+
from dates import date2num, num2date, datestr2num, strpdate2num, drange,\
240240
epoch2num, num2epoch, mx2num,\
241241
DateFormatter, IndexDateFormatter, DateLocator,\
242242
RRuleLocator, YearLocator, MonthLocator, WeekdayLocator,\

0 commit comments

Comments
 (0)