first must say, I love the mplfinance, it is a very nice way to display data on the chart.
my problem now is, I cant reduce the space to the borders. There is a parameter calle "tight_layout" but it cuts information off. Probably I do something wrong.
mpf.plot(df_history, show_nontrading=True, figratio=(10,7), figscale=1.5, datetime_format='%d.%m.%y',
xrotation=90, tight_layout=True,
alines=dict(alines=seq_of_points, colors=seq_of_colors, linestyle='-', linewidths=0.5),
type='candle', savefig=bildpfad, addplot=apdict,
update_width_config=dict(candle_linewidth=0.4))
When I use tight_layout=True
, it lookes like this:
The space around the chart is perfekt, but the data in the chart is cut off.
and if I use tight_layout=False
it uses too much space and the html file created looks crooked.
Does anybody know the right way?
There are a couple of different things you can do to fix this. First, understand the reason why this is happening. The tight_layout
algorithm sets the x-axis limits to just outside the limits of your dataframe datetime index, whereas some of your alines
points are obviously outside of this range. Given that, there are a few things you can do:
xlim=(xmin,xmax)
to manually set the x-axis limits that you want.nan
values out to the latest date that you need on your plot.tight_layout
should take alines
into account.HTH.
P.S. Presently xlim
only accepts numbers (int or float) that correspond to row numbers in your dataframe (or that correspond to matplotlib dates, see P.P.S. below). I hope to enhance xlim
to accept dates sometime soon. In the meantime, try somthing like this:
xmin = 0
xmax = len(df_history)*1.4
mpf.plot(df_history,...,xlim=(xmin,xmax))
P.P.S. I just realized that the above (xmax = len(df_history)*1.4
) will work only for show_nontrading=False
. However with show_nontrading=True
as you have it, you will need to set xmax and xmin differently, as:
import matplotlib.dates as mdates
...
# decide how many days past the end of the data
# that you want the maximum x-axis limit to be:
numdays = 10
# then:
xmin = mdates.date2num(df_history.index[0].to_py_datetime())
xmax = mdates.date2num(df_history.index[-1].to_py_datetime()) + numdays
mpf.plot(df_history,...,xlim=(xmin,xmax))
(notice above they are not both .index[0]
but xmax derives from .index[-1]
)
My apologies that the above work-arounds for xlim
are elaborate. This is motivating me even more to finish the xlim
enhancement so that users can just pass in a date as a string or datetime. Users of mplfinance should not have to worry about these date conversion details.