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

Skip to content

Commit 656edfb

Browse files
authored
Merge pull request #8157 from patniharshit/autofmtXdate
ENH: add which kwarg to autofmt_xdate
2 parents e480331 + 13028f5 commit 656edfb

File tree

3 files changed

+66
-7
lines changed

3 files changed

+66
-7
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
New which Parameter for autofmt_xdate
2+
-------------------------------------
3+
4+
A ``which`` parameter now exists for the method :func:`autofmt_xdate`. This
5+
allows a user to format ``major``, ``minor`` or ``both`` tick labels
6+
selectively. If ``which`` is ``None`` (default) then the method will rotate
7+
``major`` tick labels.
8+
9+
Example
10+
```````
11+
::
12+
13+
autofmt_xdate(self, bottom=0.2, rotation=30, ha='right', which='minor')

lib/matplotlib/figure.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ def set_tight_layout(self, tight):
441441
self._tight_parameters = tight if isinstance(tight, dict) else {}
442442
self.stale = True
443443

444-
def autofmt_xdate(self, bottom=0.2, rotation=30, ha='right'):
444+
def autofmt_xdate(self, bottom=0.2, rotation=30, ha='right', which=None):
445445
"""
446446
Date ticklabels often overlap, so it is useful to rotate them
447447
and right align them. Also, a common use case is a number of
@@ -450,29 +450,36 @@ def autofmt_xdate(self, bottom=0.2, rotation=30, ha='right'):
450450
bottom subplot and turn them off on other subplots, as well as
451451
turn off xlabels.
452452
453-
*bottom*
453+
Parameters
454+
----------
455+
456+
bottom : scalar
454457
The bottom of the subplots for :meth:`subplots_adjust`
455458
456-
*rotation*
459+
rotation : angle in degrees
457460
The rotation of the xtick labels
458461
459-
*ha*
462+
ha : string
460463
The horizontal alignment of the xticklabels
464+
465+
which : {None, 'major', 'minor', 'both'}
466+
Selects which ticklabels to rotate (default is None which works
467+
same as major)
461468
"""
462469
allsubplots = all(hasattr(ax, 'is_last_row') for ax in self.axes)
463470
if len(self.axes) == 1:
464-
for label in self.axes[0].get_xticklabels():
471+
for label in self.axes[0].get_xticklabels(which=which):
465472
label.set_ha(ha)
466473
label.set_rotation(rotation)
467474
else:
468475
if allsubplots:
469476
for ax in self.get_axes():
470477
if ax.is_last_row():
471-
for label in ax.get_xticklabels():
478+
for label in ax.get_xticklabels(which=which):
472479
label.set_ha(ha)
473480
label.set_rotation(rotation)
474481
else:
475-
for label in ax.get_xticklabels():
482+
for label in ax.get_xticklabels(which=which):
476483
label.set_visible(False)
477484
ax.set_xlabel('')
478485

lib/matplotlib/tests/test_figure.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
from matplotlib import rcParams
66
from matplotlib.testing.decorators import image_comparison
77
from matplotlib.axes import Axes
8+
from matplotlib.ticker import AutoMinorLocator, FixedFormatter
89
import matplotlib.pyplot as plt
10+
import matplotlib.dates as mdates
911
import numpy as np
1012
import warnings
13+
import pytest
1114

1215

1316
def test_figure_label():
@@ -234,3 +237,39 @@ def test_figaspect():
234237
assert h / w == 0.5
235238
w, h = plt.figaspect(np.zeros((2, 2)))
236239
assert h / w == 1
240+
241+
242+
@pytest.mark.parametrize('which', [None, 'both', 'major', 'minor'])
243+
def test_autofmt_xdate(which):
244+
date = ['3 Jan 2013', '4 Jan 2013', '5 Jan 2013', '6 Jan 2013',
245+
'7 Jan 2013', '8 Jan 2013', '9 Jan 2013', '10 Jan 2013',
246+
'11 Jan 2013', '12 Jan 2013', '13 Jan 2013', '14 Jan 2013']
247+
248+
time = ['16:44:00', '16:45:00', '16:46:00', '16:47:00', '16:48:00',
249+
'16:49:00', '16:51:00', '16:52:00', '16:53:00', '16:55:00',
250+
'16:56:00', '16:57:00']
251+
252+
angle = 60
253+
minors = [1, 2, 3, 4, 5, 6, 7]
254+
255+
x = mdates.datestr2num(date)
256+
y = mdates.datestr2num(time)
257+
258+
fig, ax = plt.subplots()
259+
260+
ax.plot(x, y)
261+
ax.yaxis_date()
262+
ax.xaxis_date()
263+
264+
ax.xaxis.set_minor_locator(AutoMinorLocator(2))
265+
ax.xaxis.set_minor_formatter(FixedFormatter(minors))
266+
267+
fig.autofmt_xdate(0.2, angle, 'right', which)
268+
269+
if which in ('both', 'major', None):
270+
for label in fig.axes[0].get_xticklabels(False, 'major'):
271+
assert int(label.get_rotation()) == angle
272+
273+
if which in ('both', 'minor'):
274+
for label in fig.axes[0].get_xticklabels(True, 'minor'):
275+
assert int(label.get_rotation()) == angle

0 commit comments

Comments
 (0)