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

Skip to content

Commit 5539a42

Browse files
committed
MNT: consolidate tick_label logic
Collect (almost) all of the logic for the bar tick_label in one place. Closes #5237
1 parent ee2e1c9 commit 5539a42

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1980,9 +1980,6 @@ def bar(self, left, height, width=0.8, bottom=None, **kwargs):
19801980
edgecolor = kwargs.pop('edgecolor', None)
19811981
linewidth = kwargs.pop('linewidth', None)
19821982

1983-
tick_label = kwargs.pop('tick_label', None)
1984-
label_ticks_flag = tick_label is not None
1985-
19861983
# Because xerr and yerr will be passed to errorbar,
19871984
# most dimension checking and processing will be left
19881985
# to the errorbar method.
@@ -1998,6 +1995,7 @@ def bar(self, left, height, width=0.8, bottom=None, **kwargs):
19981995
orientation = kwargs.pop('orientation', 'vertical')
19991996
log = kwargs.pop('log', False)
20001997
label = kwargs.pop('label', '')
1998+
tick_labels = kwargs.pop('tick_label', None)
20011999

20022000
def make_iterable(x):
20032001
if not iterable(x):
@@ -2013,7 +2011,6 @@ def make_iterable(x):
20132011
_bottom = bottom
20142012
bottom = make_iterable(bottom)
20152013
linewidth = make_iterable(linewidth)
2016-
tick_label = make_iterable(tick_label)
20172014

20182015
adjust_ylim = False
20192016
adjust_xlim = False
@@ -2058,8 +2055,6 @@ def make_iterable(x):
20582055

20592056
if len(linewidth) < nbars:
20602057
linewidth *= nbars
2061-
if len(tick_label) < nbars:
2062-
tick_label *= nbars
20632058

20642059
if color is None:
20652060
color = [None] * nbars
@@ -2092,9 +2087,6 @@ def make_iterable(x):
20922087
if len(bottom) != nbars:
20932088
raise ValueError("incompatible sizes: argument 'bottom' "
20942089
"must be length %d or scalar" % nbars)
2095-
if len(tick_label) != nbars:
2096-
raise ValueError("incompatible sizes: argument 'tick_label' "
2097-
"must be length %d or string" % nbars)
20982090

20992091
patches = []
21002092

@@ -2190,9 +2182,16 @@ def make_iterable(x):
21902182
bar_container = BarContainer(patches, errorbar, label=label)
21912183
self.add_container(bar_container)
21922184

2193-
if label_ticks_flag:
2185+
if tick_labels is not None:
2186+
tick_labels = make_iterable(tick_labels)
2187+
if not is_sequence_of_strings(tick_labels):
2188+
raise ValueError("tick_label must be a sequence of strings")
2189+
if len(tick_labels) != tick_label_position:
2190+
raise ValueError("incompatible sizes: argument 'tick_label' "
2191+
"must be length %d or string" % nbars)
2192+
21942193
tick_label_axis.set_ticks(tick_label_position)
2195-
tick_label_axis.set_ticklabels(tick_label)
2194+
tick_label_axis.set_ticklabels(tick_labels)
21962195

21972196
return bar_container
21982197

lib/matplotlib/tests/test_axes.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,12 @@ def test_bar_tick_label_single():
10551055
ax.bar("a", "b" , tick_label='a', data=data)
10561056

10571057

1058+
@cleanup
1059+
def test_bar_ticklabel_fail():
1060+
fig, ax = plt.subplots()
1061+
ax.bar([], [])
1062+
1063+
10581064
@image_comparison(baseline_images=['bar_tick_label_multiple'],
10591065
extensions=['png'])
10601066
def test_bar_tick_label_multiple():
@@ -1082,6 +1088,7 @@ def test_hist_log():
10821088
ax = fig.add_subplot(111)
10831089
ax.hist(data, fill=False, log=True)
10841090

1091+
10851092
@image_comparison(baseline_images=['hist_bar_empty'], remove_text=True,
10861093
extensions=['png'])
10871094
def test_hist_bar_empty():

0 commit comments

Comments
 (0)