3

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

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
cwfmoore
  • 225
  • 3
  • 11

1 Answers1

7

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:

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

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Daniel Goldfarb
  • 6,937
  • 5
  • 29
  • 61
  • I'm not in finance, so I only use this package when looking at questions on SO, but I can see that it's a nice piece of engineering. Again, thanks for your effort. See you around. – Trenton McKinney Apr 23 '21 at 18:09
  • 1
    It answer is a explains a lot about how to use the axes method with mpl finance. Thank you friend, saved my time and confusion. – cwfmoore Apr 24 '21 at 08:05