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

Add option for plot function to return figure and not display the figure itself. #46

Closed
MikeAMCloud opened this issue Mar 7, 2020 · 6 comments
Assignees
Labels
enhancement New feature or request released code merged into repo AND released to Pypi

Comments

@MikeAMCloud
Copy link

MikeAMCloud commented Mar 7, 2020

I desire to use mplfinance with tkinter.

To do this most straight forwardly I desire an option to return the figure.

So adding an option returnfig, using code something like the below:

Adding to vkwargs:

    'returnfig': {'Default': False,'Validator': lambda value: isinstance(value, bool)},

And extending the "savefig" code something like the below:
Code something like the below:

if config['savefig'] is not None:
    save = config['savefig']
    if isinstance(save,dict):
        plt.savefig(**save)
    else:
        plt.savefig(save)
else:
    if config['returnfig'] is not None:
        save = config['returnfig']
        if save:
            return (fig)            
    # https://stackoverflow.com/a/13361748/1639359 suggests plt.show(block=False)
    plt.show(block=config['block'])
@MikeAMCloud MikeAMCloud added the enhancement New feature or request label Mar 7, 2020
@DanielGoldfarb
Copy link
Collaborator

Your code above looks fine; I think you can simplify the else block as:

else:
    if config['returnfig']:
        return (fig)            
    # https://stackoverflow.com/a/13361748/1639359 suggests plt.show(block=False)
    plt.show(block=config['block'])

(the validator is already ensuring that config['returnfig'] is a bool, so there is no worry it's None).

I have never used tkinter so I don't know enough to say if returning the Figure will accomplish what you want it to. I suggest, if you feel comfortable, you can make the above change in your own fork, and then test it with your tkiniter code and let me know how it works out. If it accomplishes what you want it to accomplish with tkiniter, then I will consider merging it in.

@MikeAMCloud
Copy link
Author

Yes the above code does work.

An example with tkinter follows:

DrawPlot.docx

@DanielGoldfarb
Copy link
Collaborator

OK. There are enough people asking for this. I will include it in the next release. (Maybe a week; maybe two weeks). Thanks for getting back to me.

@MikeAMCloud
Copy link
Author

Thanks for the quick responses.

Although I have not tried this, it seems that if one desired to add additional subplots, one could return the figure from mplfinance and then add as many subplots as one wanted using figure.add_subplot

@DanielGoldfarb
Copy link
Collaborator

Thanks for the quick responses.

Although I have not tried this, it seems that if one desired to add additional subplots, one could return the figure from mplfinance and then add as many subplots as one wanted using figure.add_subplot

I don't think that will work, because by default the subplots that mpf.plot() creates (main panel and lower [volume] panel) take up the entire Figure (except to leave some room for the axis labels).

However, I am now 95% certain that I will implement something like what I outlined here which should allow users to create as many subplots as desire in whatever configuration they desire. (The only uncertainty is not knowing what, if any, issues I may encounter while testing it; but my preliminary experiments seem to indicate that it will work OK).

@DanielGoldfarb
Copy link
Collaborator

wip

@DanielGoldfarb DanielGoldfarb self-assigned this Mar 18, 2020
@DanielGoldfarb DanielGoldfarb added in progress merged / awaiting release to pypi code merged into repo, but not yet released to Pypi and removed in progress labels Mar 22, 2020
This was referenced Mar 25, 2020
@DanielGoldfarb DanielGoldfarb added released code merged into repo AND released to Pypi and removed merged / awaiting release to pypi code merged into repo, but not yet released to Pypi labels May 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request released code merged into repo AND released to Pypi
Projects
None yet
Development

No branches or pull requests

2 participants