Description
Bug report
Bug summary
The automatic limits for datetime axes depend in surprising ways on the presence of artists without any data.
Code for reproduction
import numpy as np
import matplotlib.pyplot as plt
Case 1:
If a datetime plot follows an empty plot, the autoscaling on the date axis are not what one would expect:
f, ax = plt.subplots()
x = np.arange("2010-01-01", "2011-01-01", dtype="datetime64[D]")
ax.scatter([], [])
ax.scatter(x, np.arange(len(x)))
While I can't account for the specific choice of limits, I gather that the first plot sets the units on the axes in a way that interferes with the subsequent plot. And indeed, we can fix the problem by setting a dtype on our null data.
Case 2:
f, ax = plt.subplots()
x = np.arange("2010-01-01", "2011-01-01", dtype="datetime64[D]")
null = np.array([], x.dtype)
ax.scatter(null, [])
ax.scatter(x, np.arange(len(x)))
But this does not work for all functions. Consider the case of fill_between
.
Case 3:
f, ax = plt.subplots()
x = np.arange("2010-01-01", "2011-01-01", dtype="datetime64[D]")
null = np.array([], x.dtype)
ax.fill_between(null, [])
ax.fill_between(x, np.arange(len(x)))
In summary, I think Case 1 is not a bug (it would be nice to defer unit setting for empty data, but I can easily imagine that this is hard/impossible). But I can't account for Case 3.
Matplotlib version
- Operating system: macos
- Matplotlib version: 3.2.1
- Matplotlib backend (
print(matplotlib.get_backend())
): pylab inline - Python version: 3.8