Create beautiful stock price charts (in 1 line of code)

May 4, 2024
Facebook logo.
Twitter logo.
LinkedIn logo.
Get this code in Google Colab

Create beautiful stock price charts (in 1 line of code)

Visualizing data is the first step to analyzing it effectively. It’s the first step in spotting trends, patterns, and anomalies that could inform your trading decisions.

Yet, most individuals attempt to build stock price charts themselves using libraries like Matplotlib, which, while powerful, can be painful.

Matplotlib has a steep learning curve and a time-consuming setup for specialized financial charts.

There’s a better way.

After reading today’s newsletter, you’ll be able to skip the complex coding part and jump straight back to what matters: analyzing the market.

Let’s go!

Create beautiful stock price charts (in 1 line of code)

mplfinance is designed for financial data visualization. It makes creating detailed candlestick, OHLC, and other financial charts easy.

It was originally part of the matplotlib library. It has since been spun off as an independent project.

mplfinance abstracts away the complexity of Matplotlib. Despite its simplicity, we still benefit from Matplotlib’s power.

mplfinance is for market data analysts who need to quickly visualize market data but don’t want to waste time with Matplotlib.

Let’s see some examples of how it works.

We’ll import yFinance to quickly download market prices.

1import yfinance as yf
2import mplfinance as mpf
3import warnings
4warnings.filterwarnings('ignore')
5
6data = yf.download("AAPL", start="2022-01-01", end="2022-06-30")

The default chart type is a simple OHLC plot.

1mpf.plot(data)

The result is a simple OHLC chart.

Create beautiful stock price charts. Visualizing data is the first step to analyzing it effectively. Do it with mplfinance.

Create beautiful stock price charts. Visualizing data is the first step to analyzing it effectively. Do it with mplfinance.

By adding the type argument, we can define the plot type.

1mpf.plot(data, type="candle")

The result is a candle chart.

Create beautiful stock price charts (in 1 line of code)

Create beautiful stock price charts. Visualizing data is the first step to analyzing it effectively. Do it with mplfinance.

Some people like simple line charts.

1mpf.plot(data, type="line")

The result is a line chart.

Create beautiful stock price charts. Visualizing data is the first step to analyzing it effectively. Do it with mplfinance.
Create beautiful stock price charts. Visualizing data is the first step to analyzing it effectively. Do it with mplfinance.

Other people like more complex charts like Renko charts.

1mpf.plot(data, type="renko")

The result is a Renko chart.

Create beautiful stock price charts. Visualizing data is the first step to analyzing it effectively. Do it with mplfinance.
Create beautiful stock price charts. Visualizing data is the first step to analyzing it effectively. Do it with mplfinance.

We can also include moving averages to the plot. Use a single number for a single moving average. Use a tuple of numbers for several.

1mpf.plot(data, type="ohlc", mav=15)

The result is an OHLC chart with a 15 period moving average.

Create beautiful stock price charts. Visualizing data is the first step to analyzing it effectively. Do it with mplfinance.
Create beautiful stock price charts. Visualizing data is the first step to analyzing it effectively. Do it with mplfinance.

Let’s include three moving averages.

1mpf.plot(data, type="candle", mav=(7, 14, 21))

The result is a candle chart with 7, 15, and 21 day moving averages.

Create beautiful stock price charts. Visualizing data is the first step to analyzing it effectively. Do it with mplfinance.
Create beautiful stock price charts. Visualizing data is the first step to analyzing it effectively. Do it with mplfinance.
1mpf.plot(data, type="candle", mav=(7, 14, 21), volume=True)

We can also include volume.

The result is a candle chart with multiple moving averages plotted with volume.

Create beautiful stock price charts. Visualizing data is the first step to analyzing it effectively. Do it with mplfinance.
Create beautiful stock price charts. Visualizing data is the first step to analyzing it effectively. Do it with mplfinance.

We can include non-trading days as well.

1mpf.plot(
2    data, 
3    type="candle", 
4    mav=(7, 14, 21), 
5    volume=True, 
6    show_nontrading=True
7)

The result is the same as above including non-trading days (note the gaps in the volume bars).

Create beautiful stock price charts. Visualizing data is the first step to analyzing it effectively. Do it with mplfinance.
Create beautiful stock price charts. Visualizing data is the first step to analyzing it effectively. Do it with mplfinance.

mplfinance handles intraday charting too.

1intraday = yf.download(tickers="PLTR", period="5d", interval="1m")
2iday = intraday.iloc[-100:, :]
3mpf.plot(iday, type="candle", mav=(7, 12), volume=True)

The result is a 1-minute chart showing the last 100 minutes of prices.

Create beautiful stock price charts (in 1 line of code)
Create beautiful stock price charts. Visualizing data is the first step to analyzing it effectively. Do it with mplfinance.

Your next steps

mplfinance has many chart types, including animations. Next time you’re analyzing asset price data, consider using mplfinance to speed up your analysis.

Man with glasses and a wristwatch, wearing a white shirt, looking thoughtfully at a laptop with a data screen in the background.