mplfinance — matplolib’s relatively unknown library for plotting financial data | by Eryk Lewinson | Towards Data Science

It is a well-known fact that matplotlib is very versatile and can be used to create pretty much any kind of chart you want. It might not be the simplest or prettiest, but after viewing enough questions on StackOverflow it will most likely work out quite well in the end.
I knew that it is possible to create financial plots such as a candlestick chart in pure matplotlib, but that is not the most pleasant experience and there are much easier ways to do it with libraries such as plotly or altair (I covered this in another article). However, only recently have I found out that there is a separate library/module of matplotlib dedicated to financial plotting. It is called mplfinance and in this article, I will show some of its nice and quite unique features.
The setup is quite standard. First, we import the libraries.
Then, we download stock prices to work with — for this article we use Apple’s stock prices from the second half of 2020. If you are interested in more details on downloading the stock prices, you can check out my other article on that.
mplfinance offers a few kinds of plots useful for analyzing patterns in asset prices. The first one, also the default one in the library, is the OHLC chart. We can create it by simply using the plot function:
mpf.plot(df["2020-12-01":])
where df is a pandas DataFrame containing OHLC data and a DatetimeIndex. We restrict the data to the last month only in order to clearly see the shape of the plot’s elements.
The interpretation is pretty similar to candlestick charts. The horizontal line on the left indicates the open price, the one on the right the close price. The vertical line represents the volatility of the prices and we can read the high/low prices from the two extremes.
At this point, it is worth mentioning that mplfinance offers an easy way to stack multiple layers of information on one chart. For example, imagine that we wanted to add the high and low prices as lines to the previously created plot. We can easily do that using the make_addplot function, as illustrated below. We first define the additional lines and then pass them as an extra argument to the plot function.
Running the code generates the following image, which only confirms that the extremes of the vertical lines correspond to the high and low prices of the given day.
Naturally, this is a simplified example. In a more complex case, we might be interested in adding some technical indicators, for example, the Bollinger Bands or a Simple Moving Average. We will get back to the latter one soon. We can also use the same functions to create symbols showing where we entered/exited a position. You can find a good example here.
The next in line of the available types of plots is the candlestick chart. Generating them with mplfinance is as simple as adding an extra argument to the plot function.
mpf.plot(df["2020-12-01":], type="candle")
When you look at the candles and the dates, it is clear that there are some missing dates there. That naturally comes from the fact that the markets are closed on the weekends and some special days. If you want to take this into account, you can provide an extra argument to the plot function:
mpf.plot(df["2020-12-01":], type="candle", show_nontrading=True)
Let’s add even more information to the plot. First, there is a handy argument we can pass into the plot function — mav — which will automatically add any simple moving averages we want. For this plot, let’s take the 10- and 20-day MAs. Secondly, we can also add the traded volume.
mpf.plot(df, type="candle", mav=(10, 20), volume=True)
To be honest, mplfinance is the first place in which I saw the following two kinds of plots, as they are not as popular as OHLC and candlestick charts. The first one is called a Renko chart and is built using price movement, without taking into account a standardized time interval as most charts do.
What it means in practice is that a new block is created when the price moves by a specified amount and each subsequent block is added at a 45-degree angle to the prior one, either above it or below.
The most common use of the Renko chart is to filter out noise from the price series and to help with identifying trends in the prices. That is because all price movements smaller than the indicated box size are filtered out.
We can create the Renko chart by simply specifying the type argument when using the plot function.
mpf.plot(df,type="renko")
We can also modify the brick’s size to our liking. In the following snippet, we set it to 2.
mpf.plot(df, type="renko", renko_params=dict(brick_size=2))
The last type of plot available in the library is the Point and Figure chart. Similarly to the Renko chart, it does not take into account the passage of time. The P&F chart uses columns of stacked X’s and O’s, where each symbol stands for a certain price movement (determined by the box size, which we can tune to our preferences).
X represents a rise in the price by a certain amount, while O stands for a drop. The last piece of information we need is the condition for creating new columns of different symbols (O’s following X’s, and vice versa). In order for a new column to be created, the price must change by the reversal amount, which is typically set to be three times the box size (in mplfinance, the default value is 1).
mpf.plot(df, type="pnf")
We can easily compare this P&F chart to the first Renko plot to see the very same patterns.
Plots created using mplfinance are already quite nice to look at for one-liners, so definitely something that does not happen that often in pure matplotlib. However, we can use a few more options available in the plot function to make our plots even prettier.
For the next plot, we change the ratio of the figure, add a title, choose the tight layout and apply a style. We use the binance style, which makes the plot similar to the ones available at the popular Crypto exchange.
Personally, I would say that this is quite an improvement for the amount of extra code we had to write. If you are curious about what styles are available in the library, you can use the following command to view them all:
mpf.available_styles()
Lastly, we can also easily save the figure to a local file. To do so, we just need to provide the file’s name to the savefig argument of the plot function. The code would look as follows.
mplfinance is a library in matplotlib’s portfolio dedicated to plotting asset price data
the API is very easy to use and we can often create nice charts with one-liners
the library offers some uncommon types of plots such as the Renko chart or the Point and Figure chart
You can find the code used for this article on my GitHub. Also, any constructive feedback is welcome. I am also curious if you have heard about Renko/Point and Figure charts and maybe even used them in practice. You can reach out to me on Twitter or in the comments.
If you are interested in learning about how to use Python for quantitative finance, you might want to check out Quantra (disclaimer: an affiliate link), which offers a variety of different courses on the topic.
Note from Towards Data Science’s editors: While we allow independent authors to publish articles in accordance with our rules and guidelines, we do not endorse each author’s contribution. You should not rely on an author’s works without seeking professional advice. See our Reader Terms for details.
If you liked this article, you might also be interested in one of the following: