Mplfinance: How to size a saved plot

Created on 24 Aug 2020  ยท  6Comments  ยท  Source: matplotlib/mplfinance

Hello! I tried to save my plot like this

    mydpi = 100
    fname = "images_market/EURUSD/H4/sample_2.png"

    saving_params = dict(fname=fname, dpi=mydpi, figsize =(324/mydpi,252/mydpi), bbox_inches='tight', pad_inches = 0.1)
    mpf.plot(sampleH4, type='candle', style='yahoo', volume=True, axisoff=True,savefig=saving_params)

I was expecting a png image with 324 pixels width and 252 pixels height but I got a png image with 595 pixels width and 422 pixels height. I don't understand, what does it means?

question

Most helpful comment

Tessan,
I'm not sure; this seems to be a Matplotlib question because mplfinance calls matplotlib.figure.Figure.savefig().

You could accomplish the same thing by doing

fig, axlist = mpf.plot(data,...,returnfig=True)
fig.savefig(...)

That said, I'm not sure if Figure.savefig() is supposed to behave exactly as you have described or not; I would have to investigate (which I believe you can do as well).

Also, I don't know if setting figsize at the time you create the Figure has any impact on figsize at the time you save the Figure; I would imagine it should not have any impact, but you can verify this by also passing figsize=(324/mydpi,252/mydpi) into the call to mpf.plot() (in addition to passing it with the savefig parameters) ...

myfigsize = (324/mydpi,252/mydpi)
saving_params = dict(fname=fname, dpi=mydpi, figsize =myfigsize, bbox_inches='tight', pad_inches = 0.1)
mpf.plot(sampleH4, type='candle', style='yahoo', volume=True, axisoff=True, figsize=myfigsize, savefig=saving_params)

Sorry I couldn't be of more assistance. If I had more time I'd run some experiments on this myself, but I'm in the middle of some other projects right now. All the best. --Daniel

All 6 comments

Tessan,
I'm not sure; this seems to be a Matplotlib question because mplfinance calls matplotlib.figure.Figure.savefig().

You could accomplish the same thing by doing

fig, axlist = mpf.plot(data,...,returnfig=True)
fig.savefig(...)

That said, I'm not sure if Figure.savefig() is supposed to behave exactly as you have described or not; I would have to investigate (which I believe you can do as well).

Also, I don't know if setting figsize at the time you create the Figure has any impact on figsize at the time you save the Figure; I would imagine it should not have any impact, but you can verify this by also passing figsize=(324/mydpi,252/mydpi) into the call to mpf.plot() (in addition to passing it with the savefig parameters) ...

myfigsize = (324/mydpi,252/mydpi)
saving_params = dict(fname=fname, dpi=mydpi, figsize =myfigsize, bbox_inches='tight', pad_inches = 0.1)
mpf.plot(sampleH4, type='candle', style='yahoo', volume=True, axisoff=True, figsize=myfigsize, savefig=saving_params)

Sorry I couldn't be of more assistance. If I had more time I'd run some experiments on this myself, but I'm in the middle of some other projects right now. All the best. --Daniel

If you use bbox_inches='tight' that will "shrink wrap" your output to only be the non-boring pixels (which means you give up the ability to exactly control the output size).

Exactly! I am experiencing that. Is there any method to control the output size and the empty blank spaces around my image at the same time?

@tessanix
mpf.plot() has a kwarg scale_padding which may work to allow you to control the blank space around the plot while controlling the output size at the same time. See this comment for information on how to use scale_padding.

thank you @DanielGoldfarb !
I used this :

fig1, _ = mpf.plot(sampleH4,
                           type='candle',
                           style='yahoo',
                           figsize =(324/mydpi,252/mydpi), 
                           volume=True,
                           axisoff=True,
                           returnfig=True, 
                           scale_padding=0.2)
fig1.savefig(fname,dpi=mydpi)

and now it works perfectly :+1:

@tessanix Great! Thanks for letting me know! ๐Ÿ‘ All the best.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

viorell91 picture viorell91  ยท  3Comments

obahat picture obahat  ยท  3Comments

leochan007 picture leochan007  ยท  6Comments

allahyarzadeh picture allahyarzadeh  ยท  3Comments

simonholzapfel picture simonholzapfel  ยท  5Comments