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

Skip to content

Commit d5e7341

Browse files
committed
More simplification of ax.bar().
1 parent 44ef533 commit d5e7341

File tree

1 file changed

+17
-54
lines changed

1 file changed

+17
-54
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 17 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2003,14 +2003,11 @@ def bar(self, left, height, width=0.8, bottom=None, **kwargs):
20032003
label = kwargs.pop('label', '')
20042004
tick_labels = kwargs.pop('tick_label', None)
20052005

2006-
# make them safe to take len() of
20072006
_left = left
2008-
left = np.atleast_1d(left)
2009-
height = np.atleast_1d(height)
2010-
width = np.atleast_1d(width)
20112007
_bottom = bottom
2012-
bottom = np.atleast_1d(bottom)
2013-
linewidth = np.atleast_1d(linewidth)
2008+
left, height, width, bottom = np.broadcast_arrays(
2009+
# Make args iterable too.
2010+
np.atleast_1d(left), height, width, bottom)
20142011

20152012
adjust_ylim = False
20162013
adjust_xlim = False
@@ -2022,11 +2019,7 @@ def bar(self, left, height, width=0.8, bottom=None, **kwargs):
20222019
if _bottom is None:
20232020
if self.get_yscale() == 'log':
20242021
adjust_ylim = True
2025-
bottom = [0]
2026-
2027-
nbars = len(left)
2028-
width = _backports_np.broadcast_to(width, nbars)
2029-
bottom = _backports_np.broadcast_to(bottom, nbars)
2022+
bottom = np.zeros_like(bottom)
20302023

20312024
tick_label_axis = self.xaxis
20322025
tick_label_position = left
@@ -2038,54 +2031,23 @@ def bar(self, left, height, width=0.8, bottom=None, **kwargs):
20382031
if _left is None:
20392032
if self.get_xscale() == 'log':
20402033
adjust_xlim = True
2041-
left = [0]
2042-
2043-
nbars = len(bottom)
2044-
left = _backports_np.broadcast_to(left, nbars)
2045-
height = _backports_np.broadcast_to(height, nbars)
2034+
left = np.zeros_like(left)
20462035

20472036
tick_label_axis = self.yaxis
20482037
tick_label_position = bottom
20492038
else:
20502039
raise ValueError('invalid orientation: %s' % orientation)
20512040

2052-
if len(height) == 1:
2053-
height *= nbars
2054-
if len(width) == 1:
2055-
width *= nbars
2056-
if len(linewidth) < nbars:
2057-
linewidth = np.tile(linewidth, nbars)
2058-
2059-
color = list(mcolors.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
2064-
2041+
linewidth = itertools.cycle(np.atleast_1d(linewidth))
2042+
color = itertools.chain(itertools.cycle(mcolors.to_rgba_array(color)),
2043+
# Fallback if color == "none".
2044+
itertools.repeat([0, 0, 0, 0]))
20652045
if edgecolor is None:
2066-
edgecolor = [None] * nbars
2046+
edgecolor = itertools.repeat(None)
20672047
else:
2068-
edgecolor = list(mcolors.to_rgba_array(edgecolor))
2069-
if len(edgecolor) == 0: # until to_rgba_array is changed
2070-
edgecolor = [[0, 0, 0, 0]]
2071-
if len(edgecolor) < nbars:
2072-
edgecolor *= nbars
2073-
2074-
# input validation
2075-
if len(left) != nbars:
2076-
raise ValueError("incompatible sizes: argument 'left' must "
2077-
"be length %d or scalar" % nbars)
2078-
if len(height) != nbars:
2079-
raise ValueError("incompatible sizes: argument 'height' "
2080-
"must be length %d or scalar" % nbars)
2081-
if len(width) != nbars:
2082-
raise ValueError("incompatible sizes: argument 'width' "
2083-
"must be length %d or scalar" % nbars)
2084-
if len(bottom) != nbars:
2085-
raise ValueError("incompatible sizes: argument 'bottom' "
2086-
"must be length %d or scalar" % nbars)
2087-
2088-
patches = []
2048+
edgecolor = itertools.chain(mcolors.to_rgba_array(edgecolor),
2049+
# Fallback if edgecolor == "none".
2050+
itertools.repeat([0, 0, 0, 0]))
20892051

20902052
# lets do some conversions now since some types cannot be
20912053
# subtracted uniformly
@@ -2103,13 +2065,14 @@ def bar(self, left, height, width=0.8, bottom=None, **kwargs):
21032065

21042066
if align == 'center':
21052067
if orientation == 'vertical':
2106-
left = [l - w / 2. for l, w in zip(left, width)]
2068+
left = left - width / 2
21072069
elif orientation == 'horizontal':
2108-
bottom = [b - h / 2. for b, h in zip(bottom, height)]
2070+
bottom = bottom - height / 2
21092071

21102072
elif align != 'edge':
21112073
raise ValueError('invalid alignment: %s' % align)
21122074

2075+
patches = []
21132076
args = zip(left, bottom, width, height, color, edgecolor, linewidth)
21142077
for l, b, w, h, c, e, lw in args:
21152078
r = mpatches.Rectangle(
@@ -2174,7 +2137,7 @@ def bar(self, left, height, width=0.8, bottom=None, **kwargs):
21742137
self.add_container(bar_container)
21752138

21762139
if tick_labels is not None:
2177-
tick_labels = _backports_np.broadcast_to(tick_labels, nbars)
2140+
tick_labels = _backports_np.broadcast_to(tick_labels, len(patches))
21782141
tick_label_axis.set_ticks(tick_label_position)
21792142
tick_label_axis.set_ticklabels(tick_labels)
21802143

0 commit comments

Comments
 (0)