Issue
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.
Solution
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
Answered By - Daniel Goldfarb
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.