Skip to content

Commit e264115

Browse files
author
y-p
committed
Merge pull request #3300 from y-p/log_bar
Work around mpl 1.2.1 regression re bar log plot GH3298, GH3254
2 parents c8728e2 + cf55a91 commit e264115

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

pandas/tests/test_graphics.py

+15
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,21 @@ def test_bar_center(self):
404404
ax = df.plot(kind='bar', grid=True)
405405
self.assertEqual(ax.xaxis.get_ticklocs()[0],
406406
ax.patches[0].get_x() + ax.patches[0].get_width())
407+
@slow
408+
def test_bar_log(self):
409+
# GH3254, GH3298 matplotlib/matplotlib#1882, #1892
410+
# regressions in 1.2.1
411+
412+
df = DataFrame({'A': [3] * 5, 'B': range(5)}, index=range(5))
413+
ax = df.plot(kind='bar', grid=True,log=True)
414+
self.assertEqual(ax.yaxis.get_ticklocs()[0],1.0)
415+
416+
p1 = Series([200,500]).plot(log=True,kind='bar')
417+
p2 = DataFrame([Series([200,300]),Series([300,500])]).plot(log=True,kind='bar',subplots=True)
418+
419+
(p1.yaxis.get_ticklocs() == np.array([ 0.625, 1.625]))
420+
(p2[0].yaxis.get_ticklocs() == np.array([ 100., 1000.])).all()
421+
(p2[1].yaxis.get_ticklocs() == np.array([ 100., 1000.])).all()
407422

408423
@slow
409424
def test_boxplot(self):

pandas/tools/plotting.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,7 @@ def __init__(self, data, **kwargs):
13251325
else:
13261326
self.tickoffset = 0.375
13271327
self.bar_width = 0.5
1328+
self.log = kwargs.pop('log',False)
13281329
MPLPlot.__init__(self, data, **kwargs)
13291330

13301331
def _args_adjust(self):
@@ -1335,9 +1336,9 @@ def _args_adjust(self):
13351336
def bar_f(self):
13361337
if self.kind == 'bar':
13371338
def f(ax, x, y, w, start=None, **kwds):
1338-
return ax.bar(x, y, w, bottom=start, **kwds)
1339+
return ax.bar(x, y, w, bottom=start,log=self.log, **kwds)
13391340
elif self.kind == 'barh':
1340-
def f(ax, x, y, w, start=None, **kwds):
1341+
def f(ax, x, y, w, start=None, log=self.log, **kwds):
13411342
return ax.barh(x, y, w, left=start, **kwds)
13421343
else:
13431344
raise NotImplementedError
@@ -1354,6 +1355,7 @@ def _get_colors(self):
13541355
return colors
13551356

13561357
def _make_plot(self):
1358+
import matplotlib as mpl
13571359
colors = self._get_colors()
13581360
rects = []
13591361
labels = []
@@ -1371,10 +1373,15 @@ def _make_plot(self):
13711373
kwds = self.kwds.copy()
13721374
kwds['color'] = colors[i % len(colors)]
13731375

1376+
# default, GH3254
1377+
# I tried, I really did.
1378+
start = 0 if mpl.__version__ == "1.2.1" else None
13741379
if self.subplots:
13751380
ax = self._get_ax(i) # self.axes[i]
1376-
rect = bar_f(ax, self.ax_pos, y,
1377-
self.bar_width, **kwds)
1381+
1382+
rect = bar_f(ax, self.ax_pos, y, self.bar_width,
1383+
start = start,
1384+
**kwds)
13781385
ax.set_title(label)
13791386
elif self.stacked:
13801387
mask = y > 0
@@ -1385,6 +1392,7 @@ def _make_plot(self):
13851392
neg_prior = neg_prior + np.where(mask, 0, y)
13861393
else:
13871394
rect = bar_f(ax, self.ax_pos + i * 0.75 / K, y, 0.75 / K,
1395+
start = start,
13881396
label=label, **kwds)
13891397
rects.append(rect)
13901398
labels.append(label)
@@ -1404,7 +1412,8 @@ def _post_plot_logic(self):
14041412
ax.set_xticks(self.ax_pos + self.tickoffset)
14051413
ax.set_xticklabels(str_index, rotation=self.rot,
14061414
fontsize=self.fontsize)
1407-
ax.axhline(0, color='k', linestyle='--')
1415+
if not self.log: # GH3254+
1416+
ax.axhline(0, color='k', linestyle='--')
14081417
if name is not None:
14091418
ax.set_xlabel(name)
14101419
else:

0 commit comments

Comments
 (0)