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

Skip to content

Commit 5a7dab7

Browse files
committed
API: bar now color cycles
repeated calls to `ax.bar` will advance the patch color cycle. closes #5854
1 parent 95cbca4 commit 5a7dab7

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1983,6 +1983,8 @@ def bar(self, left, height, width=0.8, bottom=None, **kwargs):
19831983
if not self._hold:
19841984
self.cla()
19851985
color = kwargs.pop('color', None)
1986+
if color is None:
1987+
color = self._get_patches_for_fill.get_next_color()
19861988
edgecolor = kwargs.pop('edgecolor', None)
19871989
linewidth = kwargs.pop('linewidth', None)
19881990

@@ -2062,14 +2064,11 @@ def make_iterable(x):
20622064
if len(linewidth) < nbars:
20632065
linewidth *= nbars
20642066

2065-
if color is None:
2066-
color = [None] * nbars
2067-
else:
2068-
color = list(mcolors.to_rgba_array(color))
2069-
if len(color) == 0: # until to_rgba_array is changed
2070-
color = [[0, 0, 0, 0]]
2071-
if len(color) < nbars:
2072-
color *= nbars
2067+
color = list(mcolors.to_rgba_array(color))
2068+
if len(color) == 0: # until to_rgba_array is changed
2069+
color = [[0, 0, 0, 0]]
2070+
if len(color) < nbars:
2071+
color *= nbars
20732072

20742073
if edgecolor is None:
20752074
edgecolor = [None] * nbars

lib/matplotlib/tests/test_axes.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from numpy.testing import assert_allclose, assert_array_equal
3030
import warnings
3131
from matplotlib.cbook import IgnoredKeywordWarning
32-
32+
import matplotlib.colors as mcolors
3333

3434
# Note: Some test cases are run twice: once normally and once with labeled data
3535
# These two must be defined in the same test function or need to have
@@ -4492,6 +4492,17 @@ def test_large_offset():
44924492
fig.canvas.draw()
44934493

44944494

4495+
def test_bar_color_cycle():
4496+
ccov = mcolors.colorConverter.to_rgb
4497+
fig, ax = plt.subplots()
4498+
for j in range(5):
4499+
ln, = ax.plot(range(3))
4500+
brs = ax.bar(range(3), range(3))
4501+
for br in brs:
4502+
print(ln.get_color(), br.get_facecolor())
4503+
assert ccov(ln.get_color()) == ccov(br.get_facecolor())
4504+
4505+
44954506
if __name__ == '__main__':
44964507
import nose
44974508
import sys

lib/matplotlib/tests/test_bbox_tight.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def test_bbox_inches_tight():
3434
# the bottom values for stacked bar chart
3535
fig, ax = plt.subplots(1, 1)
3636
for row in xrange(rows):
37-
plt.bar(ind, data[row], width, bottom=yoff)
37+
ax.bar(ind, data[row], width, bottom=yoff, color='b')
3838
yoff = yoff + data[row]
3939
cellText.append([''])
4040
plt.xticks([])

0 commit comments

Comments
 (0)