How can I change the formatting of the mplfinance volume on the chart?

2.1k Views Asked by At

I am using mplfinance package to plot candlestick charts of the stock. I am currently trying to figure out how can I change the formatting of the volume in the mplfinance. In all examples provided by the package, and in my own chart the volume comes out in strange notation like 1e23 etc. I would like my volume to reflect the numerical value of what is actually in the pandas dataframe. I trade myself and when I am looking at charts anywhere on the actual trading platforms, it shows normal, it actually shows the volume. But when I look at matplotlib, pandas, mplfinance examples online, the notations is formatted in a strange way everywhere.

Example of what I am talking about

2

There are 2 best solutions below

1
On BEST ANSWER

The volume notation is automatically in exponential form based on the size of the volume, so if you want to avoid this, you can avoid it by making the original data smaller with unit data. The following example shows how to deal with this problem by dividing by 1 million. This data is taken from the official website.

daily['Volume'] = daily['Volume'] / 1000000

This is how we responded.

%matplotlib inline
import pandas as pd

daily = pd.read_csv('data/SP500_NOV2019_Hist.csv',index_col=0,parse_dates=True)
daily['Volume'] = daily['Volume'] / 1000000

import mplfinance as mpf

mpf.plot(daily,type='candle',volume=True,
         title='\nS&P 500, Nov 2019',
         ylabel='OHLC Candles',
         ylabel_lower='Shares\nTraded')

enter image description here

Example of normal output

enter image description here

0
On

Alternatively, to show the volumes not in scientific notation, but keeping the original values (not scaled down) ... using the same data/code as in the answer from @r-beginners ...

fig, axlist = mpf.plot(daily,type='candle',volume=True,
                       title='\nS&P 500, Nov 2019',
                       ylabel='OHLC Candles',
                       ylabel_lower='Shares\nTraded',
                       returnfig=True)

import matplotlib.ticker as mticker
axlist[2].yaxis.set_major_formatter(mticker.FormatStrFormatter('%d'))
mpf.show()

The result: enter image description here

In theory it would be relatively easy to enhance mplfinance to accept a kwarg for formating the axis labels; but for now the above will work.