Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 96d2376

Browse files
committed
DOC/TST : document and test negative width to bar
`ax.bar` allows you to specify the left edge of the bars. The 'align' keyword allows the user to tell mpl to interpret the left edges as the centers. To use the left edges as the right edges the user can simply pass a negative width. - Added note to the 'align' entry in the docstring - Added test to make sure this does not get 'fixed' with validation on width in bar
1 parent f532615 commit 96d2376

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,6 +1855,8 @@ def bar(self, left, height, width=0.8, bottom=None, **kwargs):
18551855
If `edge`, aligns bars by their left edges (for vertical bars) and
18561856
by their bottom edges (for horizontal bars). If `center`, interpret
18571857
the `left` argument as the coordinates of the centers of the bars.
1858+
To align on the align bars on the right edge pass a negative
1859+
`width`.
18581860
18591861
orientation : 'vertical' | 'horizontal', optional, default: 'vertical'
18601862
The orientation of the bars.

lib/matplotlib/tests/test_axes.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def test_twinx_cla():
131131

132132
@image_comparison(baseline_images=["minorticks_on_rcParams_both"], extensions=['png'])
133133
def test_minorticks_on_rcParams_both():
134-
134+
135135
fig = plt.figure()
136136
matplotlib.rcParams['xtick.minor.visible'] = True
137137
matplotlib.rcParams['ytick.minor.visible'] = True
@@ -3569,24 +3569,28 @@ def test_pathological_hexbin():
35693569
fig.savefig(out)
35703570
assert_equal(len(w), 0)
35713571

3572+
35723573
@cleanup
35733574
def test_color_None():
35743575
# issue 3855
35753576
fig, ax = plt.subplots()
35763577
ax.plot([1,2], [1,2], color=None)
35773578

3579+
35783580
@cleanup
35793581
def test_color_alias():
35803582
# issues 4157 and 4162
35813583
fig, ax = plt.subplots()
35823584
line = ax.plot([0, 1], c='lime')[0]
35833585
assert_equal('lime', line.get_color())
35843586

3587+
35853588
@cleanup
35863589
def test_numerical_hist_label():
35873590
fig, ax = plt.subplots()
35883591
ax.hist([range(15)] * 5, label=range(5))
35893592

3593+
35903594
@cleanup
35913595
def test_move_offsetlabel():
35923596
data = np.random.random(10) * 1e-22
@@ -3595,6 +3599,18 @@ def test_move_offsetlabel():
35953599
ax.yaxis.tick_right()
35963600
assert_equal((1, 0.5), ax.yaxis.offsetText.get_position())
35973601

3602+
3603+
@cleanup
3604+
def test_bar_negative_width():
3605+
fig, ax = plt.subplots()
3606+
res = ax.bar(range(1, 5), range(1, 5), width=-1)
3607+
assert_equal(len(res), 4)
3608+
for indx, b in enumerate(res):
3609+
assert_equal(b._x, indx)
3610+
assert_equal(b._width, 1)
3611+
assert_equal(b._height, indx + 1)
3612+
3613+
35983614
if __name__ == '__main__':
35993615
import nose
36003616
import sys

0 commit comments

Comments
 (0)