Skip to content

Acessing mplfinance Figure and Axes objects

Daniel Goldfarb edited this page Nov 24, 2021 · 5 revisions

There are two ways to gain access to mplfinance's Figure and Axes objects:


1. set returnfig=True when calling mpf.plot(). This is the preferred way:

  fig, axlist = mpf.plot(data,...,returnfig=True,...)

axlist will be a list of axes corresponding to the panels from top to bottom, two axes per panel where the first is the primary axes and the next is the secondary axes.

For example, if you have 3 panels then returnfig=True will return 6 Axes objects:

  • axlist[0] and axlist[1] will be the primary and secondary axes, respectively, for panel 0.
  • axlist[2] and axlist[3] will be the primary and secondary axes, respectively, for panel 1, etc.

Note that normally mpf.plot() will display the plot for you; however when returnfig=True then mplfinance assumes you want to do some of your own modifications to the Figure or Axes objects before displaying them, so it does not show the figure. Therefore, when you are ready to show the figure, call mpf.show().


2. Create your own Figure and Axes objects and pass these into mplfinance. You can see examples of how to do this here.

This method is discouraged. Although it gives you greater control over the Figure and Axes objects, there are some mplfinance features that are not available (because mplfinance no longer has control over the creation of the Figure and Axes objects). This technique should only be used if you are quite familiar with Matplotlib, and if what you are trying to accomplish cannot be done in any other way (ask, maybe it can). (See also: List of Features Not Supported in External Axes Mode.)