Skip to content

Commit 08245ba

Browse files
author
Tom Augspurger
committed
Merge pull request #9914 from sinhrks/bar_log
BUG: barplot with log=True not working for values smaller than 1
2 parents 161f38d + 0f22e94 commit 08245ba

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

doc/source/whatsnew/v0.16.1.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ Bug Fixes
144144
- Bug in ``MultiIndex.sortlevel()`` results in unicode level name breaks (:issue:`9875`)
145145
- Bug in which ``groupby.transform`` incorrectly enforced output dtypes to match input dtypes. (:issue:`9807`)
146146

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

150150

151151

pandas/tests/test_graphics.py

+20
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,26 @@ def test_bar_log(self):
592592

593593
ax = Series([200, 500]).plot(log=True, kind='bar')
594594
assert_array_equal(ax.yaxis.get_ticklocs(), expected)
595+
tm.close()
596+
597+
ax = Series([200, 500]).plot(log=True, kind='barh')
598+
assert_array_equal(ax.xaxis.get_ticklocs(), expected)
599+
tm.close()
600+
601+
# GH 9905
602+
expected = np.array([1.0e-03, 1.0e-02, 1.0e-01, 1.0e+00])
603+
604+
if not self.mpl_le_1_2_1:
605+
expected = np.hstack((1.0e-04, expected, 1.0e+01))
606+
607+
ax = Series([0.1, 0.01, 0.001]).plot(log=True, kind='bar')
608+
assert_array_equal(ax.get_ylim(), (0.001, 0.10000000000000001))
609+
assert_array_equal(ax.yaxis.get_ticklocs(), expected)
610+
tm.close()
611+
612+
ax = Series([0.1, 0.01, 0.001]).plot(log=True, kind='barh')
613+
assert_array_equal(ax.get_xlim(), (0.001, 0.10000000000000001))
614+
assert_array_equal(ax.xaxis.get_ticklocs(), expected)
595615

596616
@slow
597617
def test_bar_ignore_index(self):

pandas/tools/plotting.py

+4-9
Original file line numberDiff line numberDiff line change
@@ -1824,21 +1824,19 @@ def _get_plot_function(self):
18241824
if self.kind == 'bar':
18251825
def f(ax, x, y, w, start=None, **kwds):
18261826
start = start + self.bottom
1827-
return ax.bar(x, y, w, bottom=start,log=self.log, **kwds)
1827+
return ax.bar(x, y, w, bottom=start, log=self.log, **kwds)
18281828
elif self.kind == 'barh':
1829+
18291830
def f(ax, x, y, w, start=None, log=self.log, **kwds):
18301831
start = start + self.left
1831-
return ax.barh(x, y, w, left=start, **kwds)
1832+
return ax.barh(x, y, w, left=start, log=self.log, **kwds)
18321833
else:
18331834
raise ValueError("BarPlot kind must be either 'bar' or 'barh'")
18341835

18351836
return f
18361837

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

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

18641862
start = 0
1865-
if self.log:
1863+
if self.log and (y >= 1).all():
18661864
start = 1
1867-
if any(y < 1):
1868-
# GH3254
1869-
start = 0 if mpl_le_1_2_1 else None
18701865

18711866
if self.subplots:
18721867
w = self.bar_width / 2

0 commit comments

Comments
 (0)