Skip to content

Add functionality to plot bar and barh with string labels (Implement #2516) #4266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/users/whats_new/plotting.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
Plot bar and barh with labels
`````````````````````````````

Added kwarg "tick_label" to bar and barh to support plotting bar graphs with a
text label for each bar.
Example:
bar([1, 2], [1, 1], tick_label=['bar1', 'bar2'])

Added center and frame kwargs to pie
````````````````````````````````````

Expand Down
26 changes: 26 additions & 0 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1838,6 +1838,10 @@ def bar(self, left, height, width=0.8, bottom=None, **kwargs):
linewidth; If 0, don't draw edges.
default: None

tick_label : string or array-like, optional
the tick labels of the bars
default: None

xerr : scalar or array-like, optional
if not None, will be used to generate errorbar(s) on the bar chart
default: None
Expand Down Expand Up @@ -1908,6 +1912,9 @@ def bar(self, left, height, width=0.8, bottom=None, **kwargs):
edgecolor = kwargs.pop('edgecolor', None)
linewidth = kwargs.pop('linewidth', None)

tick_label = kwargs.pop('tick_label', None)
label_ticks_flag = tick_label is not None

# Because xerr and yerr will be passed to errorbar,
# most dimension checking and processing will be left
# to the errorbar method.
Expand Down Expand Up @@ -1938,6 +1945,7 @@ def make_iterable(x):
_bottom = bottom
bottom = make_iterable(bottom)
linewidth = make_iterable(linewidth)
tick_label = make_iterable(tick_label)

adjust_ylim = False
adjust_xlim = False
Expand All @@ -1956,6 +1964,9 @@ def make_iterable(x):
width *= nbars
if len(bottom) == 1:
bottom *= nbars

tick_label_axis = self.xaxis
tick_label_position = left
elif orientation == 'horizontal':
self._process_unit_info(xdata=width, ydata=bottom, kwargs=kwargs)
if log:
Expand All @@ -1971,11 +1982,16 @@ def make_iterable(x):
left *= nbars
if len(height) == 1:
height *= nbars

tick_label_axis = self.yaxis
tick_label_position = bottom
else:
raise ValueError('invalid orientation: %s' % orientation)

if len(linewidth) < nbars:
linewidth *= nbars
if len(tick_label) < nbars:
tick_label *= nbars

if color is None:
color = [None] * nbars
Expand Down Expand Up @@ -2008,6 +2024,9 @@ def make_iterable(x):
if len(bottom) != nbars:
raise ValueError("incompatible sizes: argument 'bottom' "
"must be length %d or scalar" % nbars)
if len(tick_label) != nbars:
raise ValueError("incompatible sizes: argument 'tick_label' "
"must be length %d or string" % nbars)

patches = []

Expand Down Expand Up @@ -2103,6 +2122,10 @@ def make_iterable(x):
bar_container = BarContainer(patches, errorbar, label=label)
self.add_container(bar_container)

if label_ticks_flag:
tick_label_axis.set_ticks(tick_label_position)
tick_label_axis.set_ticklabels(tick_label)

return bar_container

@docstring.dedent_interpd
Expand Down Expand Up @@ -2148,6 +2171,9 @@ def barh(self, bottom, width, height=0.8, left=None, **kwargs):
width of bar edge(s). If None, use default
linewidth; If 0, don't draw edges.

tick_label : string or array-like, optional, default: None
the tick labels of the bars

xerr : scalar or array-like, optional, default: None
if not None, will be used to generate errorbar(s) on the bar chart

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 26 additions & 1 deletion lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ def test_twinx_cla():

@image_comparison(baseline_images=["minorticks_on_rcParams_both"], extensions=['png'])
def test_minorticks_on_rcParams_both():

fig = plt.figure()
matplotlib.rcParams['xtick.minor.visible'] = True
matplotlib.rcParams['ytick.minor.visible'] = True
Expand Down Expand Up @@ -1011,6 +1010,32 @@ def test_marker_edges():
ax.plot(x+0.2, np.sin(x), 'y.', ms=30.0, mew=2, mec='b')


@image_comparison(baseline_images=['bar_tick_label_single'],
extensions=['png'])
def test_bar_tick_label_single():
# From 2516: plot bar with array of string labels for x axis
ax = plt.gca()
ax.bar(0, 1 , tick_label='a')


@image_comparison(baseline_images=['bar_tick_label_multiple'],
extensions=['png'])
def test_bar_tick_label_multiple():
# From 2516: plot bar with array of string labels for x axis
ax = plt.gca()
ax.bar([1, 2.5], [1, 2], width=[0.2, 0.5], tick_label=['a', 'b'],
align='center')


@image_comparison(baseline_images=['barh_tick_label'],
extensions=['png'])
def test_barh_tick_label():
# From 2516: plot barh with array of string labels for y axis
ax = plt.gca()
ax.barh([1, 2.5], [1, 2], height=[0.2, 0.5], tick_label=['a', 'b'],
align='center')


@image_comparison(baseline_images=['hist_log'],
remove_text=True)
def test_hist_log():
Expand Down