8000 API: bar now color cycles · matplotlib/matplotlib@5fb3a58 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5fb3a58

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

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1975,6 +1975,8 @@ def bar(self, left, height, width=0.8, bottom=None, **kwargs):
19751975
if not self._hold:
19761976
self.cla()
19771977
color = kwargs.pop('color', None)
1978+
if color is None:
1979+
color = self._get_patches_for_fill.get_next_color()
19781980
edgecolor = kwargs.pop('edgecolor', None)
19791981
linewidth = kwargs.pop('linewidth', None)
19801982

@@ -2054,14 +2056,11 @@ def make_iterable(x):
20542056
if len(linewidth) < nbars:
20552057
linewidth *= nbars
20562058

2057-
if color is None:
2058-
color = [None] * nbars
2059-
else:
2060-
color = list(mcolors.colorConverter.to_rgba_array(color))
2061-
if len(color) == 0: # until to_rgba_array is changed
2062-
color = [[0, 0, 0, 0]]
2063-
if len(color) < nbars:
2064-
color *= nbars
2059+
color = list(mcolors.colorConverter.to_rgba_array(color))
2060+
if len(color) == 0: # until to_rgba_array is changed
2061+
color = [[0, 0, 0, 0]]
2062+
if len(color) < nbars:
2063+
color *= nbars
20652064

20662065
if edgecolor is None:
20672066
edgecolor = [None] * nbars

lib/matplotlib/tests/test_axes.py

Lines changed: 13 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
@@ -4437,6 +4437,18 @@ def test_axisbelow():
44374437
ax.set_axisbelow(setting)
44384438

44394439

4440+
@cleanup
4441+
def test_bar_color_cycle():
4442+
ccov = mcolors.colorConverter.to_rgb
4443+
fig, ax = plt.subplots()
4444+
for j in range(5):
4445+
ln, = ax.plot(range(3))
4446+
brs = ax.bar(range(3), range(3))
4447+
for br in brs:
4448+
print(ln.get_color(), br.get_facecolor())
4449+
assert ccov(ln.get_color()) == ccov(br.get_facecolor())
4450+
4451+
44404452
if __name__ == '__main__':
44414453
import nose
44424454
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)
0