Skip to content

Commit f3bbcb7

Browse files
Merge pull request #234 from DanielGoldfarb/master
External Axes Mode, and animation support
2 parents 5cd1387 + 9b1516f commit f3bbcb7

19 files changed

+4281
-133
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
---
1313

14-
# <a name="announcements"></a>Announcing Version 0.12.5 and 0.12.6 Enhancements
15-
### [Version 0.12.5 and 0.12.6 bring a number of enhancements to mplfinance. Click here for full details.](https://github.com/matplotlib/mplfinance/blob/master/RELEASE_NOTES.md)
14+
# <a name="announcements"></a>Announcing Version 0.12.7
15+
### [External Axes Mode and Animation Support.](https://github.com/matplotlib/mplfinance/blob/master/RELEASE_NOTES.md)
1616

1717
---
1818

@@ -21,13 +21,14 @@
2121
- **[The New API](https://github.com/matplotlib/mplfinance#newapi)**
2222
- **[Tutorials](https://github.com/matplotlib/mplfinance#tutorials)**
2323
- **[Basic Usage](https://github.com/matplotlib/mplfinance#usage)**
24-
- **[Customizing the Appearance of Plots](https://github.com/matplotlib/mplfinance/blob/master/markdown/customization_and_styles.md)** (**New** features June 2020)
24+
- **[Customizing the Appearance of Plots](https://github.com/matplotlib/mplfinance/blob/master/markdown/customization_and_styles.md)** (New features: June 2020)
2525
- **[Adding Your Own Technical Studies to Plots](https://github.com/matplotlib/mplfinance/blob/master/examples/addplot.ipynb)**
26-
- **[Subplots: Multiple Plots on a Single Figure](https://github.com/matplotlib/mplfinance/blob/master/markdown/subplots.md)** (**New** June 2020)
26+
- **[Subplots: Multiple Plots on a Single Figure](https://github.com/matplotlib/mplfinance/blob/master/markdown/subplots.md)** (**New features: August 2020**)
2727
- **[Price-Movement Plots (Renko, P&F, etc)](https://github.com/matplotlib/mplfinance/blob/master/examples/price-movement_plots.ipynb)**
2828
- **[Trends, Support, Resistance, and Trading Lines](https://github.com/matplotlib/mplfinance/blob/master/examples/using_lines.ipynb)**
2929
- **[Saving the Plot to a File](https://github.com/matplotlib/mplfinance/blob/master/examples/savefig.ipynb)**
30-
- **[Latest Release Info](https://github.com/matplotlib/mplfinance/blob/master/RELEASE_NOTES.md)**
30+
- **[Animation/Updating your plots in realtime](https://github.com/matplotlib/mplfinance/blob/master/markdown/animation.md)** (**New: August 2020**)
31+
- **&roarr; [Latest Release Info](https://github.com/matplotlib/mplfinance/blob/master/RELEASE_NOTES.md) &loarr;**
3132
- **[Some Background History About This Package](https://github.com/matplotlib/mplfinance#history)**
3233
- **[Old API Availability](https://github.com/matplotlib/mplfinance#oldapi)**
3334

RELEASE_NOTES.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
1+
####
2+
---
3+
4+
### <a name="v0.12.7a0"></a>v0.12.7a0 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; released 2020-08-09
5+
#### Enhancements
6+
7+
- [**External Axes Mode**](https://github.com/matplotlib/mplfinance/blob/master/markdown/subplots.md#the-matplotlib-method)
8+
- [Issue #114: Display multiple plots in a row](https://github.com/matplotlib/mplfinance/issues/114).
9+
- [Issue #209: Allow plotting on the existing AXIS](https://github.com/matplotlib/mplfinance/issues/209).
10+
11+
- [**Animation Support**](https://github.com/matplotlib/mplfinance/blob/master/markdown/animation.md):
12+
- [Issue #25: Support animation/live updating of OHLCV data plots](https://github.com/matplotlib/mplfinance/issues/25).
13+
14+
---
15+
### <a name="v0.12.6a3"></a>v0.12.6a3 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; released 2020-06-28
16+
- accept lower case column names in dataframe (i.e. 'close' is the same as 'Close') ([Issue #197](https://github.com/matplotlib/mplfinance/issues/197))
17+
---
18+
### <a name="v0.12.6a2"></a>v0.12.6a2 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; released 2020-06-28
19+
- `tight_layout` now supports adjusting Figure borders (padding) around plot. ([Issue #196](https://github.com/matplotlib/mplfinance/issues/196))
20+
---
21+
### <a name="v0.12.6a1"></a>v0.12.6a1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; released 2020-06-25
22+
- `make_mpf_style()` may set `y_on_right=None`: prevent crash by treating `None` the same as `False`. ([Issue #190](https://github.com/matplotlib/mplfinance/issues/190))
23+
24+
---
25+
126
### <a name="v0.12.6a0"></a>v0.12.6a0 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; released 2020-06-24
227
#### Enhancements
328
- **`make_addplot()`** enhancements:

examples/external_axes.ipynb

Lines changed: 778 additions & 0 deletions
Large diffs are not rendered by default.

examples/mpf_animation_demo1.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'''
2+
This file contains a simple animation demo using mplfinance "external axes mode".
3+
4+
Note that presently mplfinance does not support "blitting" (blitting makes animation
5+
more efficient). Nonetheless, the animation is efficient enough to update at least
6+
once per second, and typically more frequently depending on the size of the plot.
7+
'''
8+
import pandas as pd
9+
import mplfinance as mpf
10+
import matplotlib.animation as animation
11+
12+
idf = pd.read_csv('data/SPY_20110701_20120630_Bollinger.csv',index_col=0,parse_dates=True)
13+
idf.shape
14+
idf.head(3)
15+
idf.tail(3)
16+
df = idf.loc['2011-07-01':'2011-12-30',:]
17+
18+
fig = mpf.figure(style='charles',figsize=(7,8))
19+
ax1 = fig.add_subplot(2,1,1)
20+
ax2 = fig.add_subplot(3,1,3)
21+
22+
def animate(ival):
23+
if (20+ival) > len(df):
24+
print('no more data to plot')
25+
ani.event_source.interval *= 3
26+
if ani.event_source.interval > 12000:
27+
exit()
28+
return
29+
data = df.iloc[0:(20+ival)]
30+
ax1.clear()
31+
ax2.clear()
32+
mpf.plot(data,ax=ax1,volume=ax2,type='candle')
33+
34+
ani = animation.FuncAnimation(fig, animate, interval=250)
35+
36+
mpf.show()

examples/mpf_animation_demo2.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'''
2+
This file contains a simple animation demo using mplfinance "external axes mode".
3+
4+
In this example, instead of creating the Figure and Axes external to mplfiance,
5+
we allow mplfinance to create the Figure and Axes using its "panel method", and
6+
set kwarg `returnfig=True` so that mplfinance will return the Figure and Axes.
7+
8+
We then take those Axes and pass them back into mplfinance ("external axes mode")
9+
as part of the animation.
10+
11+
Note that presently mplfinance does not support "blitting" (blitting makes animation
12+
more efficient). Nonetheless, the animation is efficient enough to update at least
13+
once per second, and typically more frequently depending on the size of the plot.
14+
'''
15+
import pandas as pd
16+
import mplfinance as mpf
17+
import matplotlib.animation as animation
18+
19+
idf = pd.read_csv('data/SPY_20110701_20120630_Bollinger.csv',index_col=0,parse_dates=True)
20+
21+
df = idf.loc['2011-07-01':'2011-12-30',:]
22+
23+
pkwargs=dict(type='candle',mav=(10,20))
24+
25+
fig, axes = mpf.plot(df.iloc[0:20],returnfig=True,volume=True,
26+
figsize=(11,8),panel_ratios=(2,1),
27+
title='\n\nS&P 500 ETF',**pkwargs)
28+
ax1 = axes[0]
29+
ax2 = axes[2]
30+
31+
def animate(ival):
32+
if (20+ival) > len(df):
33+
print('no more data to plot')
34+
ani.event_source.interval *= 3
35+
if ani.event_source.interval > 12000:
36+
exit()
37+
return
38+
data = df.iloc[0:(20+ival)]
39+
ax1.clear()
40+
ax2.clear()
41+
mpf.plot(data,ax=ax1,volume=ax2,**pkwargs)
42+
43+
ani = animation.FuncAnimation(fig, animate, interval=200)
44+
45+
mpf.show()

examples/mpf_animation_macd.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
'''
2+
This file contains a animation demo using mplfinance "external axes mode",
3+
in which animate both the display of candlesticks as well as the display
4+
of MACD (Moving Average Convergence Divergence) visual analysis.
5+
6+
In this example, instead of creating the Figure and Axes external to mplfiance,
7+
we allow mplfinance to create the Figure and Axes using its "panel method", and
8+
set kwarg `returnfig=True` so that mplfinance will return the Figure and Axes.
9+
10+
We then take those Axes and pass them back into mplfinance ("external axes mode")
11+
as part of the animation.
12+
13+
Note that presently mplfinance does not support "blitting" (blitting makes animation
14+
more efficient). Nonetheless, the animation is efficient enough to update at least
15+
once per second, and typically more frequently depending on the size of the plot.
16+
'''
17+
import pandas as pd
18+
import mplfinance as mpf
19+
import matplotlib.animation as animation
20+
21+
mpf.__version__
22+
23+
idf = pd.read_csv('data/SPY_20110701_20120630_Bollinger.csv',index_col=0,parse_dates=True)
24+
idf.shape
25+
idf.head(3)
26+
idf.tail(3)
27+
df = idf.loc['2011-07-01':'2011-12-30',:]
28+
29+
30+
# =======
31+
# MACD:
32+
33+
df = df.iloc[0:30]
34+
35+
exp12 = df['Close'].ewm(span=12, adjust=False).mean()
36+
exp26 = df['Close'].ewm(span=26, adjust=False).mean()
37+
macd = exp12 - exp26
38+
signal = macd.ewm(span=9, adjust=False).mean()
39+
histogram = macd - signal
40+
41+
apds = [mpf.make_addplot(exp12,color='lime'),
42+
mpf.make_addplot(exp26,color='c'),
43+
mpf.make_addplot(histogram,type='bar',width=0.7,panel=1,
44+
color='dimgray',alpha=1,secondary_y=False),
45+
mpf.make_addplot(macd,panel=1,color='fuchsia',secondary_y=True),
46+
mpf.make_addplot(signal,panel=1,color='b',secondary_y=True),
47+
]
48+
49+
s = mpf.make_mpf_style(base_mpf_style='classic',rc={'figure.facecolor':'lightgray'})
50+
51+
fig, axes = mpf.plot(df,type='candle',addplot=apds,figscale=1.5,figratio=(7,5),title='\n\nMACD',
52+
style=s,volume=True,volume_panel=2,panel_ratios=(6,3,2),returnfig=True)
53+
54+
ax_main = axes[0]
55+
ax_emav = ax_main
56+
ax_hisg = axes[2]
57+
ax_macd = axes[3]
58+
ax_sign = ax_macd
59+
ax_volu = axes[4]
60+
61+
df = idf.loc['2011-07-01':'2011-12-30',:]
62+
63+
def animate(ival):
64+
if (20+ival) > len(df):
65+
print('no more data to plot')
66+
ani.event_source.interval *= 3
67+
if ani.event_source.interval > 12000:
68+
exit()
69+
return
70+
data = df.iloc[0:(30+ival)]
71+
exp12 = data['Close'].ewm(span=12, adjust=False).mean()
72+
exp26 = data['Close'].ewm(span=26, adjust=False).mean()
73+
macd = exp12 - exp26
74+
signal = macd.ewm(span=9, adjust=False).mean()
75+
histogram = macd - signal
76+
apds = [mpf.make_addplot(exp12,color='lime',ax=ax_emav),
77+
mpf.make_addplot(exp26,color='c',ax=ax_emav),
78+
mpf.make_addplot(histogram,type='bar',width=0.7,
79+
color='dimgray',alpha=1,ax=ax_hisg),
80+
mpf.make_addplot(macd,color='fuchsia',ax=ax_macd),
81+
mpf.make_addplot(signal,color='b',ax=ax_sign),
82+
]
83+
84+
for ax in axes:
85+
ax.clear()
86+
mpf.plot(data,type='candle',addplot=apds,ax=ax_main,volume=ax_volu)
87+
88+
ani = animation.FuncAnimation(fig,animate,interval=100)
89+
90+
mpf.show()

0 commit comments

Comments
 (0)