|
5 | 5 | from matplotlib import rcParams |
6 | 6 | from matplotlib.testing.decorators import image_comparison |
7 | 7 | from matplotlib.axes import Axes |
| 8 | +from matplotlib.ticker import AutoMinorLocator, FixedFormatter |
8 | 9 | import matplotlib.pyplot as plt |
| 10 | +import matplotlib.dates as mdates |
9 | 11 | import numpy as np |
10 | 12 | import warnings |
| 13 | +import pytest |
11 | 14 |
|
12 | 15 |
|
13 | 16 | def test_figure_label(): |
@@ -234,3 +237,39 @@ def test_figaspect(): |
234 | 237 | assert h / w == 0.5 |
235 | 238 | w, h = plt.figaspect(np.zeros((2, 2))) |
236 | 239 | 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