Skip to content

BUG: barplot with log=True not working for values smaller than 1 #9914

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 16, 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
4 changes: 2 additions & 2 deletions doc/source/whatsnew/v0.16.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ Bug Fixes
- Bug in ``MultiIndex.sortlevel()`` results in unicode level name breaks (:issue:`9875`)
- Bug in which ``groupby.transform`` incorrectly enforced output dtypes to match input dtypes. (:issue:`9807`)



- Bug in bar plot with ``log=True`` raises ``TypeError`` if all values are less than 1 (:issue:`9905`)
- Bug in horizontal bar plot ignores ``log=True`` (:issue:`9905`)



Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/test_graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,26 @@ def test_bar_log(self):

ax = Series([200, 500]).plot(log=True, kind='bar')
assert_array_equal(ax.yaxis.get_ticklocs(), expected)
tm.close()

ax = Series([200, 500]).plot(log=True, kind='barh')
assert_array_equal(ax.xaxis.get_ticklocs(), expected)
tm.close()

# GH 9905
expected = np.array([1.0e-03, 1.0e-02, 1.0e-01, 1.0e+00])

if not self.mpl_le_1_2_1:
expected = np.hstack((1.0e-04, expected, 1.0e+01))

ax = Series([0.1, 0.01, 0.001]).plot(log=True, kind='bar')
assert_array_equal(ax.get_ylim(), (0.001, 0.10000000000000001))
assert_array_equal(ax.yaxis.get_ticklocs(), expected)
tm.close()

ax = Series([0.1, 0.01, 0.001]).plot(log=True, kind='barh')
assert_array_equal(ax.get_xlim(), (0.001, 0.10000000000000001))
assert_array_equal(ax.xaxis.get_ticklocs(), expected)

@slow
def test_bar_ignore_index(self):
Expand Down
13 changes: 4 additions & 9 deletions pandas/tools/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -1824,21 +1824,19 @@ def _get_plot_function(self):
if self.kind == 'bar':
def f(ax, x, y, w, start=None, **kwds):
start = start + self.bottom
return ax.bar(x, y, w, bottom=start,log=self.log, **kwds)
return ax.bar(x, y, w, bottom=start, log=self.log, **kwds)
elif self.kind == 'barh':

def f(ax, x, y, w, start=None, log=self.log, **kwds):
start = start + self.left
return ax.barh(x, y, w, left=start, **kwds)
return ax.barh(x, y, w, left=start, log=self.log, **kwds)
else:
raise ValueError("BarPlot kind must be either 'bar' or 'barh'")

return f

def _make_plot(self):
import matplotlib as mpl
# mpl decided to make their version string unicode across all Python
# versions for mpl >= 1.3 so we have to call str here for python 2
mpl_le_1_2_1 = str(mpl.__version__) <= LooseVersion('1.2.1')

colors = self._get_colors()
ncolors = len(colors)
Expand All @@ -1862,11 +1860,8 @@ def _make_plot(self):
kwds['ecolor'] = mpl.rcParams['xtick.color']

start = 0
if self.log:
if self.log and (y >= 1).all():
start = 1
if any(y < 1):
# GH3254
start = 0 if mpl_le_1_2_1 else None

if self.subplots:
w = self.bar_width / 2
Expand Down