Skip to content

Commit 5ab4f2e

Browse files
committed
BUG/VIS: fix Series.hist so that users can create hist subplots without the mpl API
1 parent aeabda1 commit 5ab4f2e

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

doc/source/release.rst

+2
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ pandas 0.13
9898
with the usecols parameter (:issue: `3192`)
9999
- Fix an issue in merging blocks where the resulting DataFrame had partially
100100
set _ref_locs (:issue:`4403`)
101+
- Fixed an issue where hist subplots were being overwritten when they were
102+
called using the top level matplotlib API (:issue:`4408`)
101103

102104
pandas 0.12
103105
===========

pandas/tests/test_graphics.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -662,11 +662,24 @@ def test_hist_layout(self):
662662
# invalid format for layout
663663
self.assertRaises(ValueError, df.hist, layout=(1,))
664664

665+
@slow
666+
def test_hist_no_overlap(self):
667+
from matplotlib.pyplot import subplot, close, gcf
668+
x = Series(np.random.randn(2))
669+
y = Series(np.random.randn(2))
670+
subplot(121)
671+
x.hist()
672+
subplot(122)
673+
y.hist()
674+
fig = gcf()
675+
axes = fig.get_axes()
676+
self.assertEqual(len(axes), 2)
677+
close('all')
678+
665679
@slow
666680
def test_scatter(self):
667681
_skip_if_no_scipy()
668682

669-
df = DataFrame(np.random.randn(100, 4))
670683
df = DataFrame(np.random.randn(100, 2))
671684
import pandas.tools.plotting as plt
672685

@@ -919,7 +932,7 @@ def test_time_series_plot_color_kwargs(self):
919932
def test_time_series_plot_color_with_empty_kwargs(self):
920933
import matplotlib as mpl
921934
import matplotlib.pyplot as plt
922-
935+
923936
def_colors = mpl.rcParams['axes.color_cycle']
924937

925938
plt.close('all')

pandas/tools/plotting.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2026,7 +2026,7 @@ def hist_series(self, by=None, ax=None, grid=True, xlabelsize=None,
20262026
"""
20272027
import matplotlib.pyplot as plt
20282028

2029-
fig = kwds.get('figure', plt.gcf()
2029+
fig = kwds.get('figure', _gcf()
20302030
if plt.get_fignums() else plt.figure(figsize=figsize))
20312031
if figsize is not None and tuple(figsize) != tuple(fig.get_size_inches()):
20322032
fig.set_size_inches(*figsize, forward=True)
@@ -2036,8 +2036,8 @@ def hist_series(self, by=None, ax=None, grid=True, xlabelsize=None,
20362036
raise ValueError("The 'layout' keyword is not supported when "
20372037
"'by' is None")
20382038
if ax is None:
2039-
ax = fig.add_subplot(111)
2040-
if ax.get_figure() != fig:
2039+
ax = fig.gca()
2040+
elif ax.get_figure() != fig:
20412041
raise AssertionError('passed axis not bound to passed figure')
20422042
values = self.dropna().values
20432043

0 commit comments

Comments
 (0)