0

python ggplot is awsome, but I have trouble with a memory problem.

import pandas as pd
from ggplot import *


data = pd.DataFrame({"date": [1, 2, 3],
                     "value": [10, 20, 30]})

for i in range(30):
    gg = ggplot(aes(x='date', y='value'), data=data) + geom_point(alpha=0.5)
    print(gg)
    f = "fig{}.png".format(i)
    ggsave(f, gg)

this code displays RuntimeWarning(ggplot-0.4.7).

/lib/python2.7/site-packages/matplotlib/pyplot.py:412: RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot interface (`matplotlib.pyplot.figure`) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam `figure.max_num_figures`).

How can I delete old figures ?

kzfm
  • 163
  • 1
  • 6

1 Answers1

2

ggplot uses matplotlib's pyplot interface. You can close all the existing pyplot figures with:

# Importing this library as plt is a convention, same
# importing numpy as np, or pandas as pd
import matplotlib.pyplot as plt
plt.close('all')

ggplot is still in a pretty early stage of development, so hopefully some of these issues will get smoothed over as it gets closer to a stable release.

Marius
  • 58,213
  • 16
  • 107
  • 105
  • this works well, thank you! and "import matplotlib.pyplot as plt" line doesn't need, because "from ggplot import *" loads plt variables. – kzfm Apr 09 '14 at 03:39
  • Then you have an old version, that problem with leaked imports should be fixed in the newer version (could be that this fix is not yet in a released version). – Jan Katins Apr 09 '14 at 06:16
  • @Jasc: I think 0.5.0 was only released very recently (I think within the last week or 2), and OP is a version behind on 0.4.7. – Marius Apr 09 '14 at 06:18