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

Skip to content

Commit e2f9aaa

Browse files
author
Umair Idris
committed
Add functionality to plot bar and barh with string labels
1 parent 0f940b1 commit e2f9aaa

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,8 +1813,8 @@ def bar(self, left, height, width=0.8, bottom=None, **kwargs):
18131813
18141814
Parameters
18151815
----------
1816-
left : sequence of scalars
1817-
the x coordinates of the left sides of the bars
1816+
left : sequence of scalars or strings
1817+
the x coordinates or labels of the left sides of the bars
18181818
18191819
height : sequence of scalars
18201820
the heights of the bars
@@ -1823,7 +1823,7 @@ def bar(self, left, height, width=0.8, bottom=None, **kwargs):
18231823
the width(s) of the bars
18241824
18251825
bottom : scalar or array-like, optional, default: None
1826-
the y coordinate(s) of the bars
1826+
the y coordinate(s) or labels of the bars
18271827
18281828
color : scalar or array-like, optional
18291829
the colors of the bar faces
@@ -1945,6 +1945,11 @@ def make_iterable(x):
19451945
width *= nbars
19461946
if len(bottom) == 1:
19471947
bottom *= nbars
1948+
if nbars and cbook.is_sequence_of_strings(left):
1949+
ticks_loc = [i + width[i]/2 for i in range(nbars)]
1950+
self.xaxis.set_ticks(ticks_loc)
1951+
self.xaxis.set_ticklabels(left)
1952+
left = range(nbars)
19481953
elif orientation == 'horizontal':
19491954
self._process_unit_info(xdata=width, ydata=bottom, kwargs=kwargs)
19501955
if log:
@@ -1960,6 +1965,11 @@ def make_iterable(x):
19601965
left *= nbars
19611966
if len(height) == 1:
19621967
height *= nbars
1968+
if nbars and cbook.is_sequence_of_strings(bottom):
1969+
ticks_loc = [i + height[i]/2 for i in range(nbars)]
1970+
self.yaxis.set_ticks(ticks_loc)
1971+
self.yaxis.set_ticklabels(bottom)
1972+
bottom = range(nbars)
19631973
else:
19641974
raise ValueError('invalid orientation: %s' % orientation)
19651975

@@ -2110,7 +2120,7 @@ def barh(self, bottom, width, height=0.8, left=None, **kwargs):
21102120
Parameters
21112121
----------
21122122
bottom : scalar or array-like
2113-
the y coordinate(s) of the bars
2123+
the y coordinate(s) or labels of the bars
21142124
21152125
width : scalar or array-like
21162126
the width(s) of the bars

lib/matplotlib/tests/test_axes.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ def test_twinx_cla():
131131

132132
@image_comparison(baseline_images=["minorticks_on_rcParams_both"], extensions=['png'])
133133
def test_minorticks_on_rcParams_both():
134-
135134
fig = plt.figure()
136135
matplotlib.rcParams['xtick.minor.visible'] = True
137136
matplotlib.rcParams['ytick.minor.visible'] = True
@@ -1005,6 +1004,22 @@ def test_marker_edges():
10051004
ax.plot(x+0.2, np.sin(x), 'y.', ms=30.0, mew=2, mec='b')
10061005

10071006

1007+
@image_comparison(baseline_images=['bar_left_string_labels'],
1008+
extensions=['png'])
1009+
def test_bar_left_string_labels():
1010+
# From 2516: plot bar with array of string labels for x axis
1011+
ax = plt.gca()
1012+
ax.bar(['a', 'b', 'c'], [1, 2, 3], width=[0.1, 0.5, 0.8])
1013+
1014+
1015+
@image_comparison(baseline_images=['barh_bottom_string_labels'],
1016+
extensions=['png'])
1017+
def test_barh_bottom_string_labels():
1018+
# From 2516: plot barh with array of string labels for y axis
1019+
ax = plt.gca()
1020+
ax.barh(['a', 'b', 'c'], [1, 2, 3], height=[0.1, 0.5, 0.8])
1021+
1022+
10081023
@image_comparison(baseline_images=['hist_log'],
10091024
remove_text=True)
10101025
def test_hist_log():

0 commit comments

Comments
 (0)