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

Skip to content

Commit a6af675

Browse files
committed
Merge pull request #4266 from umairidris/bar_labels
ENH : pass list of strings to label bar/barh plots Add functionality to plot bar and barh to label bars with list strings (closes #2516)
2 parents 19224ec + c2a6da5 commit a6af675

File tree

6 files changed

+60
-1
lines changed

6 files changed

+60
-1
lines changed

doc/users/whats_new/plotting.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
Plot bar and barh with labels
2+
`````````````````````````````
3+
4+
Added kwarg "tick_label" to bar and barh to support plotting bar graphs with a
5+
text label for each bar.
6+
Example:
7+
bar([1, 2], [1, 1], tick_label=['bar1', 'bar2'])
8+
19
Added center and frame kwargs to pie
210
````````````````````````````````````
311

lib/matplotlib/axes/_axes.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1838,6 +1838,10 @@ def bar(self, left, height, width=0.8, bottom=None, **kwargs):
18381838
linewidth; If 0, don't draw edges.
18391839
default: None
18401840
1841+
tick_label : string or array-like, optional
1842+
the tick labels of the bars
1843+
default: None
1844+
18411845
xerr : scalar or array-like, optional
18421846
if not None, will be used to generate errorbar(s) on the bar chart
18431847
default: None
@@ -1908,6 +1912,9 @@ def bar(self, left, height, width=0.8, bottom=None, **kwargs):
19081912
edgecolor = kwargs.pop('edgecolor', None)
19091913
linewidth = kwargs.pop('linewidth', None)
19101914

1915+
tick_label = kwargs.pop('tick_label', None)
1916+
label_ticks_flag = tick_label is not None
1917+
19111918
# Because xerr and yerr will be passed to errorbar,
19121919
# most dimension checking and processing will be left
19131920
# to the errorbar method.
@@ -1938,6 +1945,7 @@ def make_iterable(x):
19381945
_bottom = bottom
19391946
bottom = make_iterable(bottom)
19401947
linewidth = make_iterable(linewidth)
1948+
tick_label = make_iterable(tick_label)
19411949

19421950
adjust_ylim = False
19431951
adjust_xlim = False
@@ -1956,6 +1964,9 @@ def make_iterable(x):
19561964
width *= nbars
19571965
if len(bottom) == 1:
19581966
bottom *= nbars
1967+
1968+
tick_label_axis = self.xaxis
1969+
tick_label_position = left
19591970
elif orientation == 'horizontal':
19601971
self._process_unit_info(xdata=width, ydata=bottom, kwargs=kwargs)
19611972
if log:
@@ -1971,11 +1982,16 @@ def make_iterable(x):
19711982
left *= nbars
19721983
if len(height) == 1:
19731984
height *= nbars
1985+
1986+
tick_label_axis = self.yaxis
1987+
tick_label_position = bottom
19741988
else:
19751989
raise ValueError('invalid orientation: %s' % orientation)
19761990

19771991
if len(linewidth) < nbars:
19781992
linewidth *= nbars
1993+
if len(tick_label) < nbars:
1994+
tick_label *= nbars
19791995

19801996
if color is None:
19811997
color = [None] * nbars
@@ -2008,6 +2024,9 @@ def make_iterable(x):
20082024
if len(bottom) != nbars:
20092025
raise ValueError("incompatible sizes: argument 'bottom' "
20102026
"must be length %d or scalar" % nbars)
2027+
if len(tick_label) != nbars:
2028+
raise ValueError("incompatible sizes: argument 'tick_label' "
2029+
"must be length %d or string" % nbars)
20112030

20122031
patches = []
20132032

@@ -2103,6 +2122,10 @@ def make_iterable(x):
21032122
bar_container = BarContainer(patches, errorbar, label=label)
21042123
self.add_container(bar_container)
21052124

2125+
if label_ticks_flag:
2126+
tick_label_axis.set_ticks(tick_label_position)
2127+
tick_label_axis.set_ticklabels(tick_label)
2128+
21062129
return bar_container
21072130

21082131
@docstring.dedent_interpd
@@ -2148,6 +2171,9 @@ def barh(self, bottom, width, height=0.8, left=None, **kwargs):
21482171
width of bar edge(s). If None, use default
21492172
linewidth; If 0, don't draw edges.
21502173
2174+
tick_label : string or array-like, optional, default: None
2175+
the tick labels of the bars
2176+
21512177
xerr : scalar or array-like, optional, default: None
21522178
if not None, will be used to generate errorbar(s) on the bar chart
21532179

lib/matplotlib/tests/test_axes.py

Lines changed: 26 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
@@ -1011,6 +1010,32 @@ def test_marker_edges():
10111010
ax.plot(x+0.2, np.sin(x), 'y.', ms=30.0, mew=2, mec='b')
10121011

10131012

1013+
@image_comparison(baseline_images=['bar_tick_label_single'],
1014+
extensions=['png'])
1015+
def test_bar_tick_label_single():
1016+
# From 2516: plot bar with array of string labels for x axis
1017+
ax = plt.gca()
1018+
ax.bar(0, 1 , tick_label='a')
1019+
1020+
1021+
@image_comparison(baseline_images=['bar_tick_label_multiple'],
1022+
extensions=['png'])
1023+
def test_bar_tick_label_multiple():
1024+
# From 2516: plot bar with array of string labels for x axis
1025+
ax = plt.gca()
1026+
ax.bar([1, 2.5], [1, 2], width=[0.2, 0.5], tick_label=['a', 'b'],
1027+
align='center')
1028+
1029+
1030+
@image_comparison(baseline_images=['barh_tick_label'],
1031+
extensions=['png'])
1032+
def test_barh_tick_label():
1033+
# From 2516: plot barh with array of string labels for y axis
1034+
ax = plt.gca()
1035+
ax.barh([1, 2.5], [1, 2], height=[0.2, 0.5], tick_label=['a', 'b'],
1036+
align='center')
1037+
1038+
10141039
@image_comparison(baseline_images=['hist_log'],
10151040
remove_text=True)
10161041
def test_hist_log():

0 commit comments

Comments
 (0)