Skip to content

Fix plotting memory leak #9307

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.16.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ Bug Fixes




- Bug in unequal comparisons between categorical data and a scalar, which was not in the categories (e.g. ``Series(Categorical(list("abc"), ordered=True)) > "d"``. This returned ``False`` for all elements, but now raises a ``TypeError``. Equality comparisons also now return ``False`` for ``==`` and ``True`` for ``!=``. (:issue:`9848`)
- Bug in DataFrame ``__setitem__`` when right hand side is a dictionary (:issue:`9874`)
- Bug in ``where`` when dtype is ``datetime64/timedelta64``, but dtype of other is not (:issue:`9804`)
Expand All @@ -164,3 +163,4 @@ Bug Fixes

- Fixed latex output for multi-indexed dataframes (:issue:`9778`)
- Bug causing an exception when setting an empty range using ``DataFrame.loc`` (:issue:`9596`)
- Fixed memory leak in ``AreaPlot`` and ``LinePlot`` that prevented calls to ``plt.close()`` from having any effect. (:issue:`9003`)
29 changes: 29 additions & 0 deletions pandas/tests/test_graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
from numpy.testing import assert_array_equal, assert_allclose
from numpy.testing.decorators import slow
import pandas.tools.plotting as plotting
import weakref
import gc


def _skip_if_mpl_14_or_dev_boxplot():
Expand Down Expand Up @@ -3390,6 +3392,33 @@ def test_sharey_and_ax(self):
"y label is invisible but shouldn't")


def test_memory_leak(self):
""" Check that every plot type gets properly collected. """
import matplotlib.pyplot as plt
results = {}
for kind in plotting._plot_klass.keys():
args = {}
if kind in ['hexbin', 'scatter', 'pie']:
df = self.hexbin_df
args = {'x': 'A', 'y': 'B'}
elif kind == 'area':
df = self.tdf.abs()
else:
df = self.tdf

# Use a weakref so we can see if the object gets collected without
# also preventing it from being collected
results[kind] = weakref.proxy(df.plot(kind=kind, **args))

# have matplotlib delete all the figures
plt.close('all')
# force a garbage collection
gc.collect()
for key in results:
# check that every plot was collected
with tm.assertRaises(ReferenceError):
# need to actually access something to get an error
results[key].lines

@tm.mplskip
class TestDataFrameGroupByPlots(TestPlotBase):
Expand Down
Loading