Skip to content

ENH: allow title autopositioning to be turned off - #17127

Merged
ImportanceOfBeingErnest merged 2 commits into
matplotlib:masterfrom
jklymak:enh-add-titley-rcParam
Apr 15, 2020
Merged

ENH: allow title autopositioning to be turned off#17127
ImportanceOfBeingErnest merged 2 commits into
matplotlib:masterfrom
jklymak:enh-add-titley-rcParam

Conversation

@jklymak

@jklymak jklymak commented Apr 14, 2020

Copy link
Copy Markdown
Member

PR Summary

Closes #16805

Sometimes users do not want auto positioning of the title. i.e. if they set rcParams['axes.titlepad']=-20 they probably meant relative to y=1 and do not want the title to float above y=1, which is what we do now..

Here allow users to specify y in the set_title and provide a rcParams['axes.titley']. This defaults to None for auto positioning, but if set to something else autopositioning is turned off.

Alternative to #16862

import matplotlib.pyplot as plt
fig, axs = plt.subplots(1, 2, constrained_layout=True, figsize=(5,2))
axs[0].set_title('y=1.0, pad=-10\n$\sum_{j_n} x_j$', pad=-10, y=1.)
axs[1].set_title('y=None, pad=-10\n$\sum_{j_n} x_j$', pad=-10)
plt.show()

OverhangFix

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

@jklymak jklymak added this to the v3.3.0 milestone Apr 14, 2020
@jklymak

jklymak commented Apr 14, 2020

Copy link
Copy Markdown
Member Author

ping @tacaswell and @ImportanceOfBeingErnest who had opinions on the previous...

@jklymak
jklymak force-pushed the enh-add-titley-rcParam branch from 95d6ca6 to 9bb232b Compare April 14, 2020 01:58
Comment thread lib/matplotlib/axes/_axes.py Outdated
return title.get_text()

def set_title(self, label, fontdict=None, loc=None, pad=None, **kwargs):
def set_title(self, label, fontdict=None, loc=None, y=None, pad=None,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please make y a keyword only? If not it needs to go after pad.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

made the signature: def set_title(self, label, fontdict=None, loc=None, pad=None, *, y=None, **kwargs):

@tacaswell

Copy link
Copy Markdown
Member

I think this is probably the best path forward (modulo input order). I suspect we are going to end up with some degree of un-avoidable breakage, but this gives is a way to get the old behavior back if people need it and does not introduce more implicit behavior.

@jklymak
jklymak force-pushed the enh-add-titley-rcParam branch from 161a81b to 6861192 Compare April 14, 2020 05:32
@jklymak

jklymak commented Apr 14, 2020

Copy link
Copy Markdown
Member Author

I suppose it was possible someone was explicitly setting y=1 and counting on the repositioning behaviour, but it would be a strange thing to do.

@jklymak
jklymak force-pushed the enh-add-titley-rcParam branch 2 times, most recently from ca01a53 to 3c55453 Compare April 14, 2020 15:20
@jklymak
jklymak marked this pull request as ready for review April 14, 2020 15:46
plt.show()

###########################################################################
# The vertical position is automatically chosen to avoid decorators on the

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is „decorators“ a term we commonly use? If not and because of the duplicate meaning with python decorators, I‘d just use „tick labels and axis labels“ instead.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, its used in the constrainedlayout_guide, but I wrote that, so ;-) But it should be "decorations".

We need some word for this other than "ticks, labels, extra legends, annotations, etc etc". and I think "decorations" is a reasonable choice.

@jklymak
jklymak force-pushed the enh-add-titley-rcParam branch from 3c55453 to 30e05cc Compare April 15, 2020 03:15
if y is None:
y = rcParams['axes.titley']
if y is None:
y = 1.0

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need self._autotitlepos = True here?

@jklymak jklymak Apr 15, 2020

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem here is that there are really three titles with one interface. So setting self._autotitlepos=True here would allow toggling the automatic behaviour, but that behaviour would also affect other titles. i.e.

ax.set_title('Center', y=1.0)
ax.set_title('Left', loc='left')

would turn auto positioning on again for both titles.

I think the current proposed behaviour is if you make it manual, it stays manual, leads to the least unexpected results. We could make it all more complicated, but I really don't envision folks toggling this.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, that might still confuse someone, but you're right, the alternative would be more problematic because people might want to position the different titles differently.

@ImportanceOfBeingErnest
ImportanceOfBeingErnest merged commit d06a3df into matplotlib:master Apr 15, 2020
@jklymak

jklymak commented Apr 15, 2020

Copy link
Copy Markdown
Member Author

Hmm now that I think about it, if someone manually sets the y value using the text property the title will now still be auto positioned. I guess I still think this is ok. If the user thinks they will twiddle the text property they can set it manually to start as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Titles cannot be padded to negative numbers anymore.

4 participants