matplotlib / matplotlib/mplfinance

running out of memory on loop image generation

Open
#483 13 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

question
Dominant language
Python
Stars
4.4k
Forks
678
PR merge metrics
No merged PRs in 30d

Description

Hi,
I used the following code in a loop generation plots:
```python
style = mpf.make_mpf_style(base_mpf_style="yahoo")
adp = [mpf.make_addplot(re["EMA10"], width=1.5, color="#4c8fb9"),
mpf.make_addplot(re["EMA30"], width=1.5, color="#679e64"),
mpf.make_addplot(buy2["HA-low"], type="scatter", marker="^", markersize=100, color="g"),
mpf.make_addplot(sell2["HA-high"], type="scatter", marker="v", markersize=100, color="r"),
]
mpf.plot(re, type="candle", style=style,
title=f"BOT{b} {symbol} {timeframe} {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} {Algorithm}",
addplot=adp,
volume=False,
# ylabel=symbol + " - " + timeframe,
savefig=dict(fname=filename, dpi=150, pad_inches=0.50),
)
```

It runs every 15 minutes and iterates "n" times depending on how many source vectors are available, let say from 50 to 300, but the real number of plot generate are 5 to 10.
It worked fine for hours but suddenly I got the following error:

```bash
Job "scannow (trigger: interval[0:15:00], next run at: 2021-12-16 02:45:05 CET)" raised an exception
Traceback (most recent call last):
File "/home/pi/.local/lib/python3.9/site-packages/apscheduler/executors/base.py", line 125, in run_job
retval = job.func(*job.args, **job.kwargs)
File "/home/pi/deve/deve-Multi.py", line 331, in scannow
mpf.plot(re, type="candle", style=style,
File "/home/pi/.local/lib/python3.9/site-packages/mplfinance/plotting.py", line 765, in plot
plt.savefig(**save)
File "/home/pi/.local/lib/python3.9/site-packages/matplotlib/pyplot.py", line 958, in savefig
res = fig.savefig(*args, **kwargs)
File "/home/pi/.local/lib/python3.9/site-packages/matplotlib/figure.py", line 3012, in savefig
self.canvas.print_figure(fname, **kwargs)
File "/home/pi/.local/lib/python3.9/site-packages/matplotlib/backend_bases.py", line 2314, in print_figure
result = print_method(
File "/home/pi/.local/lib/python3.9/site-packages/matplotlib/backend_bases.py", line 1643, in wrapper
return func(*args, **kwargs)
File "/home/pi/.local/lib/python3.9/site-packages/matplotlib/_api/deprecation.py", line 412, in wrapper
return func(*inner_args, **inner_kwargs)
File "/home/pi/.local/lib/python3.9/site-packages/matplotlib/backends/backend_agg.py", line 540, in print_png
FigureCanvasAgg.draw(self)
File "/home/pi/.local/lib/python3.9/site-packages/matplotlib/backends/backend_agg.py", line 431, in draw
self.renderer = self.get_renderer(cleared=True)
File "/home/pi/.local/lib/python3.9/site-packages/matplotlib/backends/backend_agg.py", line 447, in get_renderer
self.renderer = RendererAgg(w, h, self.figure.dpi)
File "/home/pi/.local/lib/python3.9/site-packages/matplotlib/backends/backend_agg.py", line 93, in __init__
self._renderer = _RendererAgg(int(width), int(height), dpi)
MemoryError: In RendererAgg: Out of memory
```

should I release the memory closing the plot in such a way? How?
thanks

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the provided Python loop and trace mplfinance/plotting.py at line 765 through Matplotlib savefig and RendererAgg. Compare the repeated plot-generation behavior with the reported MemoryError and establish a reproducible test or documented resolution for the out-of-memory failure.

Written by the indexing model from the issue text.

Assessment

Tech stack
matplotlib, python
Domain
data-visualization, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.