8000 Fix pandas DataFrame align center (#8785) · matplotlib/matplotlib@a19ed84 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit a19ed84

Browse files
ryanlaclairanntzer
authored andcommitted
Fix pandas DataFrame align center (#8785)
* Added test for issue 8767 * Fix for issue 8767 * simplified test and added test for barh * added fig.canvas.draw to make sure rendering completes
1 parent e7bc819 commit a19ed84

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2070,10 +2070,9 @@ def make_iterable(x):
20702070

20712071
if align == 'center':
20722072
if orientation == 'vertical':
2073-
left = [left[i] - width[i] / 2. for i in xrange(len(left))]
2073+
left = [l - w / 2. for l, w in zip(left, width)]
20742074
elif orientation == 'horizontal':
2075-
bottom = [bottom[i] - height[i] / 2.
2076-
for i in xrange(len(bottom))]
2075+
bottom = [b - h / 2. for b, h in zip(bottom, height)]
20772076

20782077
elif align != 'edge':
20792078
raise ValueError('invalid alignment: %s' % align)

lib/matplotlib/tests/test_axes.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4863,6 +4863,36 @@ def test_pandas_indexing_hist():
48634863
axes.hist(ser_2)
48644864

48654865

4866+
def test_pandas_bar_align_center():
4867+
# Tests fix for issue 8767
4868+
pd = pytest.importorskip('pandas')
4869+
4870+
df = pd.DataFrame({'a': range(2), 'b': range(2)})
4871+
4872+
fig, ax = plt.subplots(1)
4873+
4874+
rect = ax.bar(df.loc[df['a'] == 1, 'b'],
4875+
df.loc[df['a'] == 1, 'b'],
4876+
align='center')
4877+
4878+
fig.canvas.draw()
4879+
4880+
4881+
def test_pandas_bar_align_center():
4882+
# Tests fix for issue 8767
4883+
pd = pytest.importorskip('pandas')
4884+
4885+
df = pd.DataFrame({'a': range(2), 'b': range(2)})
4886+
4887+
fig, ax = plt.subplots(1)
4888+
4889+
rect = ax.barh(df.loc[df['a'] == 1, 'b'],
4890+
df.loc[df['a'] == 1, 'b'],
4891+
align='center')
4892+
4893+
fig.canvas.draw()
4894+
4895+
48664896
def test_axis_set_tick_params_labelsize_labelcolor():
48674897
# Tests fix for issue 4346
48684898
axis_1 = plt.subplot()

0 commit comments

Comments
 (0)
0