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

Added lightweight title option #237

Merged
merged 2 commits into from
Aug 17, 2020
Merged

Conversation

teddyrowan
Copy link
Contributor

Option for the user to have a less intense title on their plots. Defaults to the original version.

Original Title:

Original Title

Lightweight Option:

Lightweight Title

@DanielGoldfarb
Copy link
Collaborator

@teddyrowan
Teddy,
Thank you so much for contributing to mplfinance. This is a great idea. If you agree, I think it would be nice to make this more generic: As you may have noticed with some of the other kwargs in mplfinance, the kwarg accepts either a specific value, or a dict which can contain any additional modifiers for the function being called by mplfinance (in this case fig.suptitle).

The simplest way for me to explain what I have in mind is to write some of the code here. If you like the idea, then please integrate it (or similar) into your local clone and then push to your fork (which will then automatically propagate to this PR).

if config['title'] is not None:
    if config['tight_layout']:
        # IMPORTANT: 0.89 is based on the top of the top panel
        #            being at 0.18+0.7 = 0.88.  See _panels.py
        # If the value changes there, then it needs to change here.
        title_kwargs = dict(size='x-large',weight='semibold', va='bottom', y=0.89)
    else:
        title_kwargs = dict(size='x-large',weight='semibold', va='center')
    if isinstance(config['title'],dict):
        title_dict = config['title']
        if 'title' not in title_dict:
            raise ValueError('Must have "title" entry in title dict')
        else:
            title = title_dict['title']
            del title_dict['title']
        title_kwargs.update(title_dict)  # allows override default values set by mplfinance above
    else:
        title = config['title']      # config['title'] is a string
    fig.suptitle(title,**title_kwargs)

With the above code, instead of:

mpf.plot(data,...,title='MyTitle',title_lightweight=True,...)

you would do:

mpf.plot(data,...,title=dict(title='MyTitle',weight='light')....)

You can now accomplish the same thing you want and have the flexibility to choose any weight ([ 'normal' | 'bold' | 'heavy' | 'light' | 'ultrabold' | 'ultralight']) as well as to modify any other kwargs available to fig.suptitle() ([x, y,ha,va,fontsize,weight]).

Let me know what you think. If you agree to integrate the above code (please check it; I have not tested it) then you will have to also modify the validator for kwarg "title" to allow dict in addtion to str; and you can then also remove kwarg "title_lightweight".

Thanks. All the best. --Daniel

P.S. Please also move the version (src/mplfinance/_version.py) up one notch. That is why the PR check is failing. We have a check in place to make sure that every PR increases the version number a notch. Thanks.

…odify title location, alignment, size, and weight.
@teddyrowan
Copy link
Contributor Author

@DanielGoldfarb

You're right, that was a much more elegant and useful way to implement the change. All parameters for suptitle are now able to be modified and there should be no issues for old code that inputs title as a string. Your code worked great.

@DanielGoldfarb DanielGoldfarb merged commit 3af71a8 into matplotlib:master Aug 17, 2020
@DanielGoldfarb
Copy link
Collaborator

Teddy, Thank you so much for contributing to mplfinance. Wishing you all the best. --Daniel

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

Successfully merging this pull request may close these issues.

None yet

2 participants