matplotlib / matplotlib/mplfinance

The use of `returnfig=True` creates multiple figures when using ipywidgets

Open
#673 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
4.4k
Forks
678
PR merge metrics
No merged PRs in 30d

Description

**Describe the bug**
If you provide returnfig=True or use your own axes, updating the ipywidgets produces additional plots.

**To Reproduce**
this example uses 15 min klines, but that doesn't matter.

```python
@interact(
start_pos = widgets.FloatSlider(value=0, max=len(df.index) - 1 - 7 * 96, min=0, step=1),
length = widgets.FloatSlider(value=7 * 96, max=7 * 96, min=96, step=96),

)
def plot_func(start_pos, length):
start_pos, length = int(start_pos), int(length)
ldf = df.iloc[start_pos:start_pos + length]
print(ldf.index[0], ' - ', ldf.index[-1])
mpf.plot(ldf, style='yahoo', figsize=(20,5), returnfig=True)
#print([ str(ax) for ax in axlist])
```

**Expected behavior**
When using ipywidgets and updateing the slider, the reloading of the plot_func produces an additional figure each time you update the widget. This shouldn't be. When your don't use returnfig (or don't add ax and volume by your own figure) this behaviour does not accure.

jupyter lab
mplfinance 0.12.10b0

**Screenshots**
![image](https://github.com/matplotlib/mplfinance/assets/1568087/147b7e8d-c72e-4299-9f8f-0a0bb7201f3b)

Contributor guide

Open the contributing guide

Research direction

Reproduce the issue in JupyterLab using the shown plot_func with ipywidgets, mplfinance, and returnfig=True. Start by observing figure creation as the sliders update, then trace the plotting entry point and figure handling. Done means repeated widget updates leave only the current plot instead of adding figures.

Written by the indexing model from the issue text.

Assessment

Tech stack
jupyter, matplotlib, python
Domain
data-visualization
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.