Mplfinance: Is there tight layout option?

Created on 16 May 2020  路  4Comments  路  Source: matplotlib/mplfinance

Is there an option for tight layout of the plot (like plt.tight_layout())? There's plenty of white space around the plot.

enhancement question released

Most helpful comment

@smjure
Yes, and No. Presently this is not direct access to plt.tight_layout(). We hope to have this available within a month or so.

In the meantime there are a couple of alternatives that may work. If you use the savefig option of mpf.plot() you can specify bbox_inches='tight' in the dict that you pass to savefig. See details here (or if GitHub is still having issues displaying jupyter notebooks when you read this, then you can clone this repository and take a look on your local machine at the savefig notebook in the examples foloder).

The second possible workaround would be to set returnfig=True. The api, mpf.plot() will then return a tuple of Figure and list(Axes), after which you can try calling tight_layout() and show() yourself. I have not tried this option yet, but it may work.

All 4 comments

@smjure
Yes, and No. Presently this is not direct access to plt.tight_layout(). We hope to have this available within a month or so.

In the meantime there are a couple of alternatives that may work. If you use the savefig option of mpf.plot() you can specify bbox_inches='tight' in the dict that you pass to savefig. See details here (or if GitHub is still having issues displaying jupyter notebooks when you read this, then you can clone this repository and take a look on your local machine at the savefig notebook in the examples foloder).

The second possible workaround would be to set returnfig=True. The api, mpf.plot() will then return a tuple of Figure and list(Axes), after which you can try calling tight_layout() and show() yourself. I have not tried this option yet, but it may work.

new version 0.12.5 just released to Pypi contains tight_layout kwarg. Set to True for tight_layout`

dear @DanielGoldfarb , how to config tight_layout?
plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
like this guide:
https://matplotlib.org/3.3.1/tutorials/intermediate/tight_layout_guide.html

@moosavimaleki

Mplfinance had to implement its own tight_layout algorithm, because when I initially implemented using matplotlib's tight_layout feature, matplotlib complained something along the lines of "This figure includes Axes that are not compatible with tight_layout, so its results might be incorrect" and, _in fact, the results were incorrect_.

In mplfinance, the tight_layout kwarg accepts only True or False.

If you are not satisfied with the resulting tight layout, you can alternatively (or together with tight_layout=True) use kwarg scale_padding, which is described here.

Let me know if that works for you, or if you have any more questions.

All the best. --Daniel

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jainraje picture jainraje  路  4Comments

mattcambs picture mattcambs  路  3Comments

obahat picture obahat  路  3Comments

ismailbayram picture ismailbayram  路  4Comments

cleitonmoya picture cleitonmoya  路  5Comments