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

Skip to content

Commit 0bfb832

Browse files
committed
add which kwarg to autofmt_xdate
1 parent 4a2c850 commit 0bfb832

3 files changed

Lines changed: 53 additions & 8 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The autofmt_xdate of the figure.Figure class previously formatted only major tick labels but now formats both minor and major tick labels by passing the argument "which = {None, 'minor', 'major', 'both'}".

lib/matplotlib/figure.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ def set_tight_layout(self, tight):
439439
self._tight_parameters = tight if isinstance(tight, dict) else {}
440440
self.stale = True
441441

442-
def autofmt_xdate(self, bottom=0.2, rotation=30, ha='right'):
442+
def autofmt_xdate(self, bottom=0.2, rotation=30, ha='right', which=None):
443443
"""
444444
Date ticklabels often overlap, so it is useful to rotate them
445445
and right align them. Also, a common use case is a number of
@@ -448,29 +448,35 @@ def autofmt_xdate(self, bottom=0.2, rotation=30, ha='right'):
448448
bottom subplot and turn them off on other subplots, as well as
449449
turn off xlabels.
450450
451-
*bottom*
451+
Parameters
452+
----------
453+
454+
bottom : scalar
452455
The bottom of the subplots for :meth:`subplots_adjust`
453456
454-
*rotation*
457+
rotation : angle in degrees
455458
The rotation of the xtick labels
456459
457-
*ha*
460+
ha : string
458461
The horizontal alignment of the xticklabels
462+
463+
which : {None, 'major', 'minor', 'both'}
464+
Selects which ticklabels to rotate
459465
"""
460466
allsubplots = all(hasattr(ax, 'is_last_row') for ax in self.axes)
461467
if len(self.axes) == 1:
462-
for label in self.axes[0].get_xticklabels():
468+
for label in self.axes[0].get_xticklabels(which=which):
463469
label.set_ha(ha)
464470
label.set_rotation(rotation)
465471
else:
466472
if allsubplots:
467473
for ax in self.get_axes():
468474
if ax.is_last_row():
469-
for label in ax.get_xticklabels():
475+
for label in ax.get_xticklabels(which=which):
470476
label.set_ha(ha)
471477
label.set_rotation(rotation)
472478
else:
473-
for label in ax.get_xticklabels():
479+
for label in ax.get_xticklabels(which=which):
474480
label.set_visible(False)
475481
ax.set_xlabel('')
476482

@@ -1838,7 +1844,6 @@ def ginput(self, n=1, timeout=30, show_clicks=True, mouse_add=1,
18381844
18391845
Right clicking cancels last input.
18401846
1841-
The buttons used for the various actions (adding points, removing
18421847
points, terminating the inputs) can be overriden via the
18431848
arguments *mouse_add*, *mouse_pop* and *mouse_stop*, that give
18441849
the associated mouse button: 1 for left, 2 for middle, 3 for

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', '11 Jan 2013',
246+
'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', '16:56:00',
250+
'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 == 'both') or (which == 'major') or (which is None)):
270+
for label in fig.axes[0].get_xticklabels(False, 'major'):
271+
assert (int(label.get_rotation()) == angle)
272+
273+
if(which == 'both' or which == 'minor'):
274+
for label in fig.axes[0].get_xticklabels(True, 'minor'):
275+
assert (int(label.get_rotation()) == angle)

0 commit comments

Comments
 (0)