Skip to content

Commit 4adc6fa

Browse files
sinhrksTomAugspurger
authored and
TomAugspurger
committed
BUG: bar plot can now handle bottom and left kw
1 parent 6c9c14e commit 4adc6fa

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

doc/source/v0.14.1.txt

+2
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,5 @@ Bug Fixes
6767

6868
- Bug in ``Index.min`` and ``max`` doesn't handle ``nan`` and ``NaT`` properly (:issue:`7261`)
6969
- Bug in ``TimeGrouper`` doesn't exclude column specified by ``key`` (:issue:`7227`)
70+
- Bug in ``DataFrame`` and ``Series`` bar and barh plot raises ``TypeError`` when ``bottom``
71+
and ``left`` keyword is specified (:issue:`7226`)

pandas/tests/test_graphics.py

+29
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,35 @@ def test_bar_barwidth_position(self):
11341134
self._check_bar_alignment(df, kind='bar', subplots=True, width=0.9, position=0.2)
11351135
self._check_bar_alignment(df, kind='barh', subplots=True, width=0.9, position=0.2)
11361136

1137+
@slow
1138+
def test_bar_bottom_left(self):
1139+
df = DataFrame(rand(5, 5))
1140+
ax = df.plot(kind='bar', stacked=False, bottom=1)
1141+
result = [p.get_y() for p in ax.patches]
1142+
self.assertEqual(result, [1] * 25)
1143+
1144+
ax = df.plot(kind='bar', stacked=True, bottom=[-1, -2, -3, -4, -5])
1145+
result = [p.get_y() for p in ax.patches[:5]]
1146+
self.assertEqual(result, [-1, -2, -3, -4, -5])
1147+
1148+
ax = df.plot(kind='barh', stacked=False, left=np.array([1, 1, 1, 1, 1]))
1149+
result = [p.get_x() for p in ax.patches]
1150+
self.assertEqual(result, [1] * 25)
1151+
1152+
ax = df.plot(kind='barh', stacked=True, left=[1, 2, 3, 4, 5])
1153+
result = [p.get_x() for p in ax.patches[:5]]
1154+
self.assertEqual(result, [1, 2, 3, 4, 5])
1155+
1156+
axes = df.plot(kind='bar', subplots=True, bottom=-1)
1157+
for ax in axes:
1158+
result = [p.get_y() for p in ax.patches]
1159+
self.assertEqual(result, [-1] * 5)
1160+
1161+
axes = df.plot(kind='barh', subplots=True, left=np.array([1, 1, 1, 1, 1]))
1162+
for ax in axes:
1163+
result = [p.get_x() for p in ax.patches]
1164+
self.assertEqual(result, [1] * 5)
1165+
11371166
@slow
11381167
def test_plot_scatter(self):
11391168
df = DataFrame(randn(6, 4),

pandas/tools/plotting.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -1789,6 +1789,9 @@ def __init__(self, data, **kwargs):
17891789
kwargs['align'] = kwargs.pop('align', 'center')
17901790
self.tick_pos = np.arange(len(data))
17911791

1792+
self.bottom = kwargs.pop('bottom', None)
1793+
self.left = kwargs.pop('left', None)
1794+
17921795
self.log = kwargs.pop('log',False)
17931796
MPLPlot.__init__(self, data, **kwargs)
17941797

@@ -1808,13 +1811,21 @@ def _args_adjust(self):
18081811
if self.rot is None:
18091812
self.rot = self._default_rot[self.kind]
18101813

1811-
@property
1812-
def bar_f(self):
1814+
if com.is_list_like(self.bottom):
1815+
self.bottom = np.array(self.bottom)
1816+
if com.is_list_like(self.left):
1817+
self.left = np.array(self.left)
1818+
1819+
def _get_plot_function(self):
18131820
if self.kind == 'bar':
18141821
def f(ax, x, y, w, start=None, **kwds):
1822+
if self.bottom is not None:
1823+
start = start + self.bottom
18151824
return ax.bar(x, y, w, bottom=start,log=self.log, **kwds)
18161825
elif self.kind == 'barh':
18171826
def f(ax, x, y, w, start=None, log=self.log, **kwds):
1827+
if self.left is not None:
1828+
start = start + self.left
18181829
return ax.barh(x, y, w, left=start, **kwds)
18191830
else:
18201831
raise NotImplementedError
@@ -1830,10 +1841,8 @@ def _make_plot(self):
18301841
colors = self._get_colors()
18311842
ncolors = len(colors)
18321843

1833-
bar_f = self.bar_f
1834-
1844+
bar_f = self._get_plot_function()
18351845
pos_prior = neg_prior = np.zeros(len(self.data))
1836-
18371846
K = self.nseries
18381847

18391848
for i, (label, y) in enumerate(self._iter_data()):

0 commit comments

Comments
 (0)