|
11 | 11 | import matplotlib.pyplot as plt |
12 | 12 | from matplotlib.cbook import MatplotlibDeprecationWarning |
13 | 13 | import matplotlib.dates as mdates |
| 14 | +import matplotlib.ticker as mticker |
14 | 15 |
|
15 | 16 |
|
16 | 17 | def __has_pytz(): |
@@ -220,6 +221,38 @@ def test_DateFormatter(): |
220 | 221 | fig.autofmt_xdate() |
221 | 222 |
|
222 | 223 |
|
| 224 | +def test_locator_set_formatter(): |
| 225 | + """ |
| 226 | + Test if setting the locator only will update the AutoDateFormatter to use |
| 227 | + the new locator. |
| 228 | + """ |
| 229 | + plt.rcParams["date.autoformatter.minute"] = "%d %H:%M" |
| 230 | + t = [datetime.datetime(2018, 9, 30, 8, 0), |
| 231 | + datetime.datetime(2018, 9, 30, 8, 59), |
| 232 | + datetime.datetime(2018, 9, 30, 10, 30)] |
| 233 | + x = [2, 3, 1] |
| 234 | + |
| 235 | + fig, ax = plt.subplots() |
| 236 | + ax.plot(t, x) |
| 237 | + ax.xaxis.set_major_locator(mdates.MinuteLocator((0, 30))) |
| 238 | + fig.canvas.draw() |
| 239 | + ticklabels = [tl.get_text() for tl in ax.get_xticklabels()] |
| 240 | + expected = ['30 08:00', '30 08:30', '30 09:00', |
| 241 | + '30 09:30', '30 10:00', '30 10:30'] |
| 242 | + assert ticklabels == expected |
| 243 | + |
| 244 | + ax.xaxis.set_major_locator(mticker.NullLocator()) |
| 245 | + ax.xaxis.set_minor_locator(mdates.MinuteLocator((5, 55))) |
| 246 | + decoy_loc = mdates.MinuteLocator((12, 27)) |
| 247 | + ax.xaxis.set_minor_formatter(mdates.AutoDateFormatter(decoy_loc)) |
| 248 | + |
| 249 | + ax.xaxis.set_minor_locator(mdates.MinuteLocator((15, 45))) |
| 250 | + fig.canvas.draw() |
| 251 | + ticklabels = [tl.get_text() for tl in ax.get_xticklabels(which="minor")] |
| 252 | + expected = ['30 08:15', '30 08:45', '30 09:15', '30 09:45', '30 10:15'] |
| 253 | + assert ticklabels == expected |
| 254 | + |
| 255 | + |
223 | 256 | def test_date_formatter_strftime(): |
224 | 257 | """ |
225 | 258 | Tests that DateFormatter matches datetime.strftime, |
|
0 commit comments