Skip to content

Commit 042b886

Browse files
authored
Merge pull request matplotlib#25667 from jklymak/fix-bar-datetime
Fix bar datetime
2 parents f1e0274 + 7b6b069 commit 042b886

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2285,12 +2285,21 @@ def bar(self, x, height, width=0.8, bottom=None, *, align="center",
22852285
height : float or array-like
22862286
The height(s) of the bars.
22872287
2288+
Note that if *bottom* has units (e.g. datetime), *height* should be in
2289+
units that are a difference from the value of *bottom* (e.g. timedelta).
2290+
22882291
width : float or array-like, default: 0.8
22892292
The width(s) of the bars.
22902293
2294+
Note that if *x* has units (e.g. datetime), then *width* should be in
2295+
units that are a difference (e.g. timedelta) around the *x* values.
2296+
22912297
bottom : float or array-like, default: 0
22922298
The y coordinate(s) of the bottom side(s) of the bars.
22932299
2300+
Note that if *bottom* has units, then the y-axis will get a Locator and
2301+
Formatter appropriate for the units (e.g. dates, or categorical).
2302+
22942303
align : {'center', 'edge'}, default: 'center'
22952304
Alignment of the bars to the *x* coordinates:
22962305
@@ -2416,13 +2425,19 @@ def bar(self, x, height, width=0.8, bottom=None, *, align="center",
24162425
x = 0
24172426

24182427
if orientation == 'vertical':
2428+
# It is possible for y (bottom) to contain unit information.
2429+
# However, it is also possible for y=0 for the default and height
2430+
# to contain unit information. This will prioritize the units of y.
24192431
self._process_unit_info(
2420-
[("x", x), ("y", height)], kwargs, convert=False)
2432+
[("x", x), ("y", y), ("y", height)], kwargs, convert=False)
24212433
if log:
24222434
self.set_yscale('log', nonpositive='clip')
24232435
else: # horizontal
2436+
# It is possible for x (left) to contain unit information.
2437+
# However, it is also possible for x=0 for the default and width
2438+
# to contain unit information. This will prioritize the units of x.
24242439
self._process_unit_info(
2425-
[("x", width), ("y", y)], kwargs, convert=False)
2440+
[("x", x), ("x", width), ("y", y)], kwargs, convert=False)
24262441
if log:
24272442
self.set_xscale('log', nonpositive='clip')
24282443

@@ -2582,12 +2597,21 @@ def barh(self, y, width, height=0.8, left=None, *, align="center",
25822597
width : float or array-like
25832598
The width(s) of the bars.
25842599
2600+
Note that if *left* has units (e.g. datetime), *width* should be in
2601+
units that are a difference from the value of *left* (e.g. timedelta).
2602+
25852603
height : float or array-like, default: 0.8
25862604
The heights of the bars.
25872605
2606+
Note that if *y* has units (e.g. datetime), then *height* should be in
2607+
units that are a difference (e.g. timedelta) around the *y* values.
2608+
25882609
left : float or array-like, default: 0
25892610
The x coordinates of the left side(s) of the bars.
25902611
2612+
Note that if *left* has units, then the x-axis will get a Locator and
2613+
Formatter appropriate for the units (e.g. dates, or categorical).
2614+
25912615
align : {'center', 'edge'}, default: 'center'
25922616
Alignment of the base to the *y* coordinates*:
25932617

lib/matplotlib/tests/test_axes.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1909,6 +1909,22 @@ def test_bar_timedelta():
19091909
(10, 20))
19101910

19111911

1912+
def test_bar_datetime_start():
1913+
"""test that tickers are correct for datetimes"""
1914+
start = np.array([np.datetime64('2012-01-01'), np.datetime64('2012-02-01'),
1915+
np.datetime64('2012-01-15')])
1916+
stop = np.array([np.datetime64('2012-02-07'), np.datetime64('2012-02-13'),
1917+
np.datetime64('2012-02-12')])
1918+
1919+
fig, ax = plt.subplots()
1920+
ax.bar([0, 1, 3], height=stop-start, bottom=start)
1921+
assert isinstance(ax.yaxis.get_major_formatter(), mdates.AutoDateFormatter)
1922+
1923+
fig, ax = plt.subplots()
1924+
ax.barh([0, 1, 3], width=stop-start, left=start)
1925+
assert isinstance(ax.xaxis.get_major_formatter(), mdates.AutoDateFormatter)
1926+
1927+
19121928
def test_boxplot_dates_pandas(pd):
19131929
# smoke test for boxplot and dates in pandas
19141930
data = np.random.rand(5, 2)

0 commit comments

Comments
 (0)