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

Skip to content

Fix bar labels #5239

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 3 commits into from
Oct 17, 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
30 changes: 14 additions & 16 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
from matplotlib import unpack_labeled_data

import matplotlib.cbook as cbook
from matplotlib.cbook import mplDeprecation, STEP_LOOKUP_MAP
from matplotlib.cbook import (mplDeprecation, STEP_LOOKUP_MAP,
iterable, is_string_like)
import matplotlib.collections as mcoll
import matplotlib.colors as mcolors
import matplotlib.contour as mcontour
Expand Down Expand Up @@ -43,10 +44,6 @@

rcParams = matplotlib.rcParams

iterable = cbook.iterable
is_string_like = cbook.is_string_like
is_sequence_of_strings = cbook.is_sequence_of_strings


def _plot_args_replacer(args, data):
if len(args) == 1:
Expand Down Expand Up @@ -1983,9 +1980,6 @@ 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 All @@ -2001,6 +1995,7 @@ def bar(self, left, height, width=0.8, bottom=None, **kwargs):
orientation = kwargs.pop('orientation', 'vertical')
log = kwargs.pop('log', False)
label = kwargs.pop('label', '')
tick_labels = kwargs.pop('tick_label', None)

def make_iterable(x):
if not iterable(x):
Expand All @@ -2016,7 +2011,6 @@ 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 Down Expand Up @@ -2061,8 +2055,6 @@ def make_iterable(x):

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 @@ -2095,9 +2087,6 @@ 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 @@ -2193,9 +2182,18 @@ def make_iterable(x):
bar_container = BarContainer(patches, errorbar, label=label)
self.add_container(bar_container)

if label_ticks_flag:
if tick_labels is not None:
tick_labels = make_iterable(tick_labels)
if isinstance(tick_labels, six.string_types):
tick_labels = [tick_labels]
if len(tick_labels) == 1:
tick_labels *= nbars
if len(tick_labels) != nbars:
raise ValueError("incompatible sizes: argument 'tick_label' "
"must be length %d or string" % nbars)

tick_label_axis.set_ticks(tick_label_position)
tick_label_axis.set_ticklabels(tick_label)
tick_label_axis.set_ticklabels(tick_labels)

return bar_container

Expand Down
7 changes: 7 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,12 @@ def test_bar_tick_label_single():
ax.bar("a", "b" , tick_label='a', data=data)


@cleanup
def test_bar_ticklabel_fail():
fig, ax = plt.subplots()
ax.bar([], [])


@image_comparison(baseline_images=['bar_tick_label_multiple'],
extensions=['png'])
def test_bar_tick_label_multiple():
Expand Down Expand Up @@ -1082,6 +1088,7 @@ def test_hist_log():
ax = fig.add_subplot(111)
ax.hist(data, fill=False, log=True)


@image_comparison(baseline_images=['hist_bar_empty'], remove_text=True,
extensions=['png'])
def test_hist_bar_empty():
Expand Down