<jupyter_start><jupyter_text>--- How to use your own `matplotlib` `Figure` and `Axes` in `mplfinance`--- &bull; Using your own `matplotlib` `Figure` and `Axes` makes your code more complex,&nbsp;&nbsp; compared to direct use of `mplfinance`,&nbsp;&nbsp; but it provides all the power and flexibility of `matplotlib`&nbsp;&nbsp; for those who know how and want to use it.- When you pass a `matplotbib` `Axes` into `mplfinance`,&nbsp;&nbsp; we say that `mplfinance` is in **External Axes Mode.**  - Certain `mplfinance` features may be not available,&nbsp;&nbsp; or may behave differently, when in External Axes Mode (see below for details).---<jupyter_code># This allows multiple outputs from a single jupyter notebook cell:
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
%matplotlib inline
import pandas as pd
idf = pd.read_csv('data/SPY_20110701_20120630_Bollinger.csv',index_col=0,parse_dates=True)
idf.shape
idf.head(3)
idf.tail(3)<jupyter_output><empty_output><jupyter_text>Let's grab 5 months of data from the input data frame:<jupyter_code>df = idf.loc['2011-07-01':'2011-12-30',:]<jupyter_output><empty_output><jupyter_text>We can, of course, plot a basic ohlc or candlestick plot:<jupyter_code>import mplfinance as mpf
mpf.__version__
mpf.plot(df,volume=True,tight_layout=True,figscale=0.75)<jupyter_output><empty_output><jupyter_text>--- Basic Introduction to External Axes Mode:---**`mplfinance`** provides a wrapper function to create a **`Figure`:** - [**`mpf.figure()`**](https://matplotlib.org/3.3.0/api/_as_gen/matplotlib.pyplot.figure.htmlmatplotlib-pyplot-figure)  This function behaves exactly like its matplotlib `pyplot` equivalent, with the added feature that it accepts **`style=`** kwarg, and embeds `mplfinance` style information into the Figure that it creates.---**Once you have created a Figure, you can call standard matplotlib Figure methods for creating subplot Axes:**- [**`fig.add_subplot()`**](https://matplotlib.org/3.3.0/api/_as_gen/matplotlib.figure.Figure.htmlmatplotlib.figure.Figure.add_subplot)- [**`fig.add_axes()`**](https://matplotlib.org/3.3.0/api/_as_gen/matplotlib.figure.Figure.htmlmatplotlib.figure.Figure.add_axes)- [**`fig.subplots()`**](https://matplotlib.org/3.3.0/api/_as_gen/matplotlib.figure.Figure.htmlmatplotlib.figure.Figure.subplots)Regarding the above three Figure methods, if the Figure was created using `mplfinance`, then these methods will apply style information to the Subplot Axes they create, based on the mplfinance style information found inside the Figure. ***Alternatively*** the user may pass **`style=`** into these Figure methods to apply a style to the Subplot Axes that may be different from the style that was applied to the Figure. ------ EXAMPLE 1Create a figure, and two subplot Axes: one for the OHLC data, and one for Volume.**NOTE** that the style information need only be applied when creating the Figure.  The same style information then propagates throughout subplot Axes creation and into `mpf.plot()`, unless overridden with kwarg `style=` in these latter methods.<jupyter_code>%%capture
fig = mpf.figure(style='yahoo',figsize=(7,8))
ax1 = fig.add_subplot(2,1,1)
ax2 = fig.add_subplot(2,1,2)
mpf.plot(df,ax=ax1,volume=ax2)
fig<jupyter_output><empty_output><jupyter_text>---**Notice** above that we display the plot by simply saying,**`fig`**This is because the code is executed in a jupyter notebook **cell**.  In a normal python script you would call **`mpf.show()`** or `plt.show()`. ------ EXAMPLE 2Create a figure, (with `default` style) and create four subplot Axes each with a different style.<jupyter_code>fig = mpf.figure(figsize=(12,9))
ax1 = fig.add_subplot(2,2,1,style='blueskies')
ax2 = fig.add_subplot(2,2,2,style='yahoo')

s   = mpf.make_mpf_style(base_mpl_style='fast',base_mpf_style='nightclouds')
ax3 = fig.add_subplot(2,2,3,style=s)

ax4 = fig.add_subplot(2,2,4,style='starsandstripes')
mpf.plot(df,ax=ax1,axtitle='blueskies',xrotation=15)
mpf.plot(df,type='candle',ax=ax2,axtitle='yahoo',xrotation=15)
mpf.plot(df,ax=ax3,type='candle',axtitle='nightclouds')
mpf.plot(df,type='candle',ax=ax4,axtitle='starsandstripes')
fig<jupyter_output><empty_output><jupyter_text>------ EXAMPLE 3Three candle plots side-by-side<jupyter_code>aapl = pd.read_csv('data/yahoofinance-AAPL-20040819-20180120.csv',index_col='Date',parse_dates=True)
goog = pd.read_csv('data/yahoofinance-GOOG-20040819-20180120.csv',index_col='Date',parse_dates=True)
spy  = pd.read_csv('data/yahoofinance-SPY-20080101-20180101.csv',index_col='Date',parse_dates=True)
aapl = aapl.loc['2016-05-01':'2016-07-31']
goog = goog.loc['2016-05-01':'2016-07-31']
spy  = spy.loc['2016-05-01':'2016-07-31']
fig = mpf.figure(figsize=(9,6),style='yahoo')
ax1 = fig.add_subplot(2,3,1)
ax2 = fig.add_subplot(2,3,2)
ax3 = fig.add_subplot(2,3,3)

av1 = fig.add_subplot(3,3,7,sharex=ax1)
av2 = fig.add_subplot(3,3,8,sharex=ax1)
av3 = fig.add_subplot(3,3,9,sharex=ax3)
mpf.plot(aapl,type='candle',ax=ax1,volume=av1,mav=(10,20),axtitle='AAPL')
mpf.plot(goog,type='candle',ax=ax2,volume=av2,mav=(10,20),axtitle='GOOG')
mpf.plot(spy ,type='candle',ax=ax3,volume=av3,mav=(10,20),axtitle='SPY')
fig<jupyter_output><empty_output><jupyter_text>------ EXAMPLE 4OHLC Bars, and Candle, on same plot:- First using addplot- Second using External Axes, **which allows us to use a different style for the OHLC bars versus Candles**<jupyter_code>ap = mpf.make_addplot(spy,type='ohlc')
mpf.plot(aapl,type='candle',style='yahoo',addplot=ap)
s = mpf.make_mpf_style(base_mpl_style='seaborn',rc={'axes.grid':False})
fig = mpf.figure(style=s,figsize=(7.5,5.75))
ax1 = fig.subplot()
ax2 = ax1.twinx()
mpf.plot(spy ,ax=ax1,type='ohlc',style='default')
mpf.plot(aapl,ax=ax2,type='candle',style='yahoo')<jupyter_output><empty_output><jupyter_text>--- Example 5 - passing external axes into addplot---<jupyter_code>%%capture
fig = mpf.figure(style='blueskies',figsize=(7,8))
ax1 = fig.add_subplot(2,1,1)
ax2 = ax1.twinx()
ax4 = fig.add_subplot(2,1,2)
ap = mpf.make_addplot(df[['UpperB','LowerB']],ax=ax2,ylabel='Bollinger Bands')
mpf.plot(df,ax=ax1,volume=ax4,addplot=ap,xrotation=10,type='candle')
fig
%%capture
fig = mpf.figure(style='charles',figsize=(7,8))
ax1 = fig.add_subplot(3,1,1)
ax2 = fig.add_subplot(3,1,2,sharex=ax1)
ax3 = fig.add_subplot(3,1,3)
ap = [ mpf.make_addplot(df,type='ohlc',ax=ax2,ylabel='OHLC Price'),
       mpf.make_addplot(df[['UpperB','LowerB']],ax=ax1)
     ]
mpf.plot(df,ax=ax1,volume=ax3,addplot=ap,xrotation=10,type='candle')
fig<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1606083650190114, 'fraction_numerical': 0.04349809885931559, 'mean_word_length': 7.088560885608856, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '520315', 'license': 'Apache-2.0', 'n_tokens_mistral': 2671, 'n_tokens_neox': 2487, 'n_words': 505, 'path': 'examples/external_axes.ipynb', 'repo_name': 'fadeawaylove/stock-trade-system', 'size': '6575'}