8000 Change bar labels to be passed as a kwarg. Fix autoscale with bar lab… · matplotlib/matplotlib@c04bf79 · GitHub
[go: up one dir, main page]

Skip to content

Commit c04bf79

Browse files
author
Umair Idris
committed
Change bar labels to be passed as a kwarg. Fix autoscale with bar labels. Add support for single string label.
1 parent 1627e8e commit c04bf79

File tree

7 files changed

+44
-27
lines changed

7 files changed

+44
-27
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1802,7 +1802,8 @@ def step(self, x, y, *args, **kwargs):
18021802
return self.plot(x, y, *args, **kwargs)
18031803

18041804
@docstring.dedent_interpd
1805-
def bar(self, left, height, width=0.8, bottom=None, **kwargs):
1805+
def bar(self, left, height, width=0.8, bottom=None, tick_label=None,
1806+
**kwargs):
18061807
"""
18071808
Make a bar plot.
18081809
@@ -1813,8 +1814,8 @@ def bar(self, left, height, width=0.8, bottom=None, **kwargs):
18131814
18141815
Parameters
18151816
----------
1816-
left : sequence of scalars or strings
1817-
the x coordinates or labels of the left sides of the bars
1817+
left : sequence of scalars
1818+
the x coordinates of the left sides of the bars
18181819
18191820
height : sequence of scalars
18201821
the heights of the bars
@@ -1823,7 +1824,10 @@ def bar(self, left, height, width=0.8, bottom=None, **kwargs):
18231824
the width(s) of the bars
18241825
18251826
bottom : scalar or array-like, optional, default: None
1826-
the y coordinate(s) or labels of the bars
1827+
the y coordinate(s) of the bars
1828+
1829+
tick_label : string or array-like, optional, default: None
1830+
the tick labels of the bars
18271831
18281832
color : scalar or array-like, optional
18291833
the colors of the bar faces
@@ -1926,6 +1930,8 @@ def make_iterable(x):
19261930
width = make_iterable(width)
19271931
_bottom = bottom
19281932
bottom = make_iterable(bottom)
1933+
_tick_label = tick_label
1934+
tick_label = make_iterable(tick_label)
19291935
linewidth = make_iterable(linewidth)
19301936

19311937
adjust_ylim = False
@@ -1945,14 +1951,8 @@ def make_iterable(x):
19451951
width *= nbars
19461952
if len(bottom) == 1:
19471953
bottom *= nbars
1948-
if nbars and cbook.is_sequence_of_strings(left):
1949-
ticks_loc = [i + w/2 for i, w in enumerate(width)]
1950-
self.xaxis.set_ticks(ticks_loc)
1951-
self.xaxis.set_ticklabels(left)
1952-
left = np.arange(nbars)
1953-
if nbars and cbook.is_sequence_of_strings(bottom):
1954-
raise ValueError('bottom must be scalar or sequence of scalar '
1955-
'for vertical bar')
1954+
1955+
tick_label_position = left
19561956
elif orientation == 'horizontal':
19571957
self._process_unit_info(xdata=width, ydata=bottom, kwargs=kwargs)
19581958
if log:
@@ -1968,17 +1968,13 @@ def make_iterable(x):
19681968
left *= nbars
19691969
if len(height) == 1:
19701970
height *= nbars
1971-
if nbars and cbook.is_sequence_of_strings(bottom):
1972-
ticks_loc = [i + h/2 for i, h in enumerate(height)]
1973-
self.yaxis.set_ticks(ticks_loc)
1974-
self.yaxis.set_ticklabels(bottom)
1975-
bottom = np.arange(nbars)
1976-
if nbars and cbook.is_sequence_of_strings(left):
1977-
raise ValueError('left must be scalar or sequence of scalar '
1978-
'for horizontal bar')
1971+
1972+
tick_label_position = bottom
19791973
else:
19801974
raise ValueError('invalid orientation: %s' % orientation)
19811975

1976+
if len(tick_label) == 1:
1977+
tick_label *= nbars
19821978
if len(linewidth) < nbars:
19831979
linewidth *= nbars
19841980

@@ -2013,6 +2009,9 @@ def make_iterable(x):
20132009
if len(bottom) != nbars:
20142010
raise ValueError("incompatible sizes: argument 'bottom' "
20152011
"must be length %d or scalar" % nbars)
2012+
if len(tick_label) != nbars:
2013+
raise ValueError("incompatible sizes: argument 'tick_label' "
2014+
"must be length %d or string" % nbars)
20162015

20172016
patches = []
20182017

@@ -2108,6 +2107,11 @@ def make_iterable(x):
21082107
bar_container = BarContainer(patches, errorbar, label=label)
21092108
self.add_container(bar_container)
21102109

2110+
if _tick_label is not None:
2111+
axis = self.xaxis if orientation == 'vertical' else self.yaxis
2112+
axis.set_ticks(tick_label_position)
2113+
axis.set_ticklabels(tick_label)
2114+
21112115
return bar_container
21122116

21132117
@docstring.dedent_interpd
@@ -2126,7 +2130,7 @@ def barh(self, bottom, width, height=0.8, left=None, **kwargs):
21262130
Parameters
21272131
----------
21282132
bottom : scalar or array-like
2129-
the y coordinate(s) or labels of the bars
2133+
the y coordinate(s) of the bars
21302134
21312135
width : scalar or array-like
21322136
the width(s) of the bars
@@ -2153,6 +2157,9 @@ def barh(self, bottom, width, height=0.8, left=None, **kwargs):
21532157
width of bar edge(s). If None, use default
21542158
linewidth; If 0, don't draw edges.
21552159
2160+
tick_label : string or array-like, optional, default: None
2161+
the tick labels of the bars
2162+
21562163
xerr : scalar or array-like, optional, default: None
21572164
if not None, will be used to generate errorbar(s) on the bar chart
21582165
Binary file not shown.
Loading
Loading
Binary file not shown.
Loading

lib/matplotlib/tests/test_axes.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,20 +1004,30 @@ def test_marker_edges():
10041004
ax.plot(x+0.2, np.sin(x), 'y.', ms=30.0, mew=2, mec='b')
10051005

10061006

1007-
@image_comparison(baseline_images=['bar_left_string_labels'],
1007+
@image_comparison(baseline_images=['bar_tick_label_single'],
10081008
extensions=['png'])
1009-
def test_bar_left_string_labels():
1009+
def test_bar_tick_label_single():
10101010
# From 2516: plot bar with array of string labels for x axis
10111011
ax = plt.gca()
1012-
ax.bar(['a', 'b', 'c'], [1, 2, 3], width=[0.1, 0.5, 0.8])
1012+
ax.bar(0, 1 , tick_label='a')
10131013

10141014

1015-
@image_comparison(baseline_images=['barh_bottom_string_labels'],
1015+
@image_comparison(baseline_images=['bar_tick_label_multiple'],
10161016
extensions=['png'])
1017-
def test_barh_bottom_string_labels():
1017+
def test_bar_tick_label_multiple():
1018+
# From 2516: plot bar with array of string labels for x axis
1019+
ax = plt.gca()
1020+
ax.bar([1, 2.5], [1, 2], width=[0.2, 0.5], tick_label=['a', 'b'],
1021+
align='center')
1022+
1023+
1024+
@image_comparison(baseline_images=['barh_tick_label'],
1025+
extensions=['png'])
1026+
def test_barh_tick_label():
10181027
# From 2516: plot barh with array of string labels for y axis
10191028
ax = plt.gca()
1020-
ax.barh(['a', 'b', 'c'], [1, 2, 3], height=[0.1, 0.5, 0.8])
1029+
ax.barh([1, 2.5], [1, 2], height=[0.2, 0.5], tick_label=['a', 'b'],
1030+
align='center')
10211031

10221032

10231033
@image_comparison(baseline_images=['hist_log'],

0 commit comments

Comments
 (0)
0