Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotib Finance (mplfinance) formatting axes of chart unsing mpf.plot()

The MPL finance is great, however I cant seem to tweak the formatting of the axes. In the image I would like to show only the date, without the 00:00 time. Also the price, I would like to add a $ currency and decimal places (variable).

import pandas as pd
import mplfinance as mpf

df = pd.read_csv(csv)
df.date = pd.to_datetime(df.date)
cols = ['date', 'open', 'high', 'low', 'close', 'volume']
df = df[cols]
df = df.sort_values(by=['date'], ascending=False)
df = df.set_index('date')

And then calling mplfinance with (inserting style):

mpf.plot(df, type='candle', volume=True style= *style*)

Generates the below charts, I have highlight the parts I would like to change if possible.

enter image description here

like image 960
cwfmoore Avatar asked Jan 29 '26 06:01

cwfmoore


1 Answers

For the date format, you can add kwarg datetime_format, for example:

mpf.plot(df, type='candle', volume=True, style=s, datetime_format='%b %d')

For the y-axis tick labels, I would suggest that you simply adjust the y-axis label (not the tick labels) using kwarg ylabel='Price ($)' or something like that.


Alternatively if you really want a $ sign next to each tick label, you can do this:

  • first gain access to the mplfinance axes objects.
  • Then set the formatter for that axes, as follows:
from matplotlib.ticker import FormatStrFormatter

fig, axlist = mpf.plot(df,type='candle',volume=True,style=s,
                       datetime_format='%b %d',returnfig=True)

axlist[0].yaxis.set_major_formatter(FormatStrFormatter('$%.2f'))

mpf.show()

Result with Default Style

enter image description here

like image 73
Daniel Goldfarb Avatar answered Jan 31 '26 21:01

Daniel Goldfarb