Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to size a saved plot #250

Closed
tessanix opened this issue Aug 24, 2020 · 6 comments
Closed

How to size a saved plot #250

tessanix opened this issue Aug 24, 2020 · 6 comments
Labels
question Further information is requested

Comments

@tessanix
Copy link

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?

@tessanix tessanix added the question Further information is requested label Aug 24, 2020
@DanielGoldfarb
Copy link
Collaborator

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

@tacaswell
Copy link
Member

tacaswell commented Aug 25, 2020

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).

@tessanix
Copy link
Author

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?

@DanielGoldfarb
Copy link
Collaborator

@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.

@tessanix
Copy link
Author

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 👍

@DanielGoldfarb
Copy link
Collaborator

@tessanix Great! Thanks for letting me know! 👍 All the best.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants