Skip to content

Commit 5030474

Browse files
committed
BUG: off-center grid in case of stacked bar plot #2157
1 parent adc9238 commit 5030474

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

pandas/tests/test_graphics.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,21 @@ def test_plot_bar(self):
288288
df = DataFrame({'a': [0, 1], 'b': [1, 0]})
289289
_check_plot_works(df.plot, kind='bar')
290290

291+
@slow
292+
def test_bar_stacked_center(self):
293+
#GH2157
294+
df = DataFrame({'A' : [3] * 5, 'B' : range(5)}, index = range(5))
295+
ax = df.plot(kind='bar', stacked='True', grid=True)
296+
self.assertEqual(ax.xaxis.get_ticklocs()[0],
297+
ax.patches[0].get_x() + ax.patches[0].get_width() / 2)
298+
299+
@slow
300+
def test_bar_center(self):
301+
df = DataFrame({'A' : [3] * 5, 'B' : range(5)}, index = range(5))
302+
ax = df.plot(kind='bar', grid=True)
303+
self.assertEqual(ax.xaxis.get_ticklocs()[0],
304+
ax.patches[0].get_x() + ax.patches[0].get_width())
305+
291306
@slow
292307
def test_boxplot(self):
293308
df = DataFrame(np.random.randn(6, 4),

pandas/tools/plotting.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,10 @@ class BarPlot(MPLPlot):
10831083
def __init__(self, data, **kwargs):
10841084
self.stacked = kwargs.pop('stacked', False)
10851085
self.ax_pos = np.arange(len(data)) + 0.25
1086+
if self.stacked:
1087+
self.tickoffset = 0.25
1088+
else:
1089+
self.tickoffset = 0.375
10861090
MPLPlot.__init__(self, data, **kwargs)
10871091

10881092
def _args_adjust(self):
@@ -1149,7 +1153,7 @@ def _post_plot_logic(self):
11491153
name = self._get_index_name()
11501154
if self.kind == 'bar':
11511155
ax.set_xlim([self.ax_pos[0] - 0.25, self.ax_pos[-1] + 1])
1152-
ax.set_xticks(self.ax_pos + 0.375)
1156+
ax.set_xticks(self.ax_pos + self.tickoffset)
11531157
ax.set_xticklabels(str_index, rotation=self.rot,
11541158
fontsize=self.fontsize)
11551159
ax.axhline(0, color='k', linestyle='--')
@@ -1158,7 +1162,7 @@ def _post_plot_logic(self):
11581162
else:
11591163
# horizontal bars
11601164
ax.set_ylim([self.ax_pos[0] - 0.25, self.ax_pos[-1] + 1])
1161-
ax.set_yticks(self.ax_pos + 0.375)
1165+
ax.set_yticks(self.ax_pos + self.tickoffset)
11621166
ax.set_yticklabels(str_index, rotation=self.rot,
11631167
fontsize=self.fontsize)
11641168
ax.axvline(0, color='k', linestyle='--')

0 commit comments

Comments
 (0)