From 3b169d6513eba1c9fd82b2d10c4747e65d6062e9 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Wed, 16 Nov 2016 08:59:20 -0500 Subject: [PATCH] FIX: better handle margins on ax.bar Cancel margin on the proper sides depending on if the bar is positive, negative, or mixed. --- lib/matplotlib/axes/_axes.py | 56 +++++++++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 7 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 59ccbc7fc910..9a95ad74750e 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -2080,6 +2080,55 @@ def make_iterable(x): raise ValueError("incompatible sizes: argument 'bottom' " "must be length %d or scalar" % nbars) + margins = {} + + if orientation == 'vertical': + # base case of 'simple' bar plot + use_bottom_margin = False + use_top_margin = True + print(use_bottom_margin, use_top_margin) + # if negative height bars, do not pin the bottom + if any(h < 0 for h in height): + use_bottom_margin = True + use_top_margin = True + print(use_bottom_margin, use_top_margin) + + # if all bars are negative pin top, but not bottom + if all(h < 0 for h in height): + use_bottom_margin = True + use_top_margin = False + + print(use_bottom_margin, use_top_margin) + # if non-trivial bottom, do not pin the bottom + if _bottom is not None: + use_bottom_margin = True + use_top_margin = True + print(use_bottom_margin, use_top_margin) + margins = {'bottom': use_bottom_margin, + 'top': use_top_margin} + + elif orientation == 'horizontal': + # base case of 'simple' bar plot + use_left_margin = False + use_right_margin = True + # if negative height bars, do not pin the bottom + if any(w < 0 for w in width): + use_left_margin = True + use_right_margin = True + + # if all bars are negative pin top, but not bottom + if all(w < 0 for w in width): + use_left_margin = True + use_right_margin = False + + # if non-trivial left, do not pin the anything + if _left is not None: + use_left_margin = True + use_right_margin = True + + margins = {'left': use_left_margin, + 'right': use_right_margin} + patches = [] # lets do some conversions now since some types cannot be @@ -2096,13 +2145,6 @@ def make_iterable(x): if yerr is not None: yerr = self.convert_yunits(yerr) - margins = {} - - if orientation == 'vertical': - margins = {'bottom': False} - elif orientation == 'horizontal': - margins = {'left': False} - if align == 'center': if orientation == 'vertical': left = [left[i] - width[i] / 2. for i in xrange(len(left))]