Skip to content

Fix: axis, ticks are set to defaults fontsize after ax.clear() - #21253

Merged
oscargus merged 6 commits into
matplotlib:mainfrom
tsumli:bugfix/axis-label
Aug 21, 2022
Merged

oscargus merged 6 commits into
matplotlib:mainfrom
tsumli:bugfix/axis-label

Conversation

@tsumli

@tsumli tsumli commented Oct 1, 2021

Copy link
Copy Markdown
Contributor

PR Summary

To solve this issue, not only content but its fontsize will be defaulted value in clear method.

PR Checklist

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (run flake8 on changed files to check).
  • New features are documented, with examples if plot related.
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • Conforms to Matplotlib style conventions (install flake8-docstrings and run flake8 --docstring-convention=all).
  • New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a while, please feel free to ping @matplotlib/developers or anyone who has commented on the PR. Most of our reviewers are volunteers and sometimes things fall through the cracks.

You can also join us on gitter for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide

We strive to be a welcoming and open project. Please follow our Code of Conduct.

Comment thread lib/matplotlib/axis.py Outdated
"""

self.label.set_text('') # self.set_label_text would change isDefault_
self.label.set_fontsize(mpl.rcParams['font.size'])

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.

I suppose we should create a new Text instance here. There are a lot more parameters that could have been set and would need resetting.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your review. I pushed a new commit. A Text instance is created in clear and default values are set.

@tacaswell

Copy link
Copy Markdown
Member

This PR is affected by a re-writing of our history to remove a large number of accidentally committed files see discourse for details.

To recover this PR it will need be rebased onto the new default branch (main). There are several ways to accomplish this, but we recommend (assuming that you call the matplotlib/matplotlib remote "upstream"

git remote update
git checkout main
git merge --ff-only upstream/main
git checkout YOUR_BRANCH
git rebase --onto=main upstream/old_master
# git rebase -i main # if you prefer
git push --force-with-lease   # assuming you are tracking your branch

If you do not feel comfortable doing this or need any help please reach out to any of the Matplotlib developers. We can either help you with the process or do it for you.

Thank you for your contributions to Matplotlib and sorry for the inconvenience.

@jklymak
jklymak marked this pull request as draft November 22, 2021 08:00
@jklymak

jklymak commented Nov 22, 2021

Copy link
Copy Markdown
Member

I've popped this to draft until the rebase can be done. @tsumli let us know if you need help with that!

@tsumli

tsumli commented Nov 22, 2021

Copy link
Copy Markdown
Contributor Author

I've done rebase. Do I have other things to do? The problem seems to be fixed.

@jklymak
jklymak marked this pull request as ready for review November 22, 2021 11:36
@jklymak
jklymak requested a review from timhoffm November 22, 2021 13:49
@tacaswell tacaswell added this to the v3.6.0 milestone Nov 22, 2021
@tacaswell

Copy link
Copy Markdown
Member

At the risk of disagreeing with @timhoffm , I have concerns about replacing the text instance. If someone has done

fig, ax = plt.subplots()
lab = ax.xaxis.label
ax.cla()
lab.set_text('bob')

this would currently work and would not work under this proposal. At a minimum this will need an API change note (in behavior) . I do not think we generically guarantee that any object hanging off of the axes from before cla() / clear() is going to remain attached after the clear so I do not think this concern should block the change, require a deprecation window, or be worth a complex work-around, but we do need to document it as a change.

If we are going to remove the old Text object we should also make sure to strip any of the references to the Axis/Axes off of it.

@jklymak

jklymak commented Feb 1, 2022

Copy link
Copy Markdown
Member

@timhoffm @tacaswell is there more discussion to be had here? Personally I find cla to be one of our more ambiguous concepts.

@timhoffm

Copy link
Copy Markdown
Member

Sorry for the late reply.

While, I don't think we have given any guarantees that label objects are persistent across clears (ticks labels e.g. obviously aren't), we can jump an extra hoop to maintain that behavior.

In that case I suggest to reset all the following text attributes (with the exception of self._renderer):

self._text = ''
self.set_text(text)
self.set_color(
color if color is not None else mpl.rcParams["text.color"])
self.set_fontproperties(fontproperties)
self.set_usetex(usetex)
self.set_parse_math(parse_math if parse_math is not None else
mpl.rcParams['text.parse_math'])
self.set_wrap(wrap)
self.set_verticalalignment(verticalalignment)
self.set_horizontalalignment(horizontalalignment)
self._multialignment = multialignment
self.set_rotation(rotation)
self._transform_rotates_text = transform_rotates_text
self._bbox_patch = None # a FancyBboxPatch instance
self._renderer = None
if linespacing is None:
linespacing = 1.2 # Maybe use rcParam later.
self._linespacing = linespacing
self.set_rotation_mode(rotation_mode)

by creating a Text._reset_visual_defaults() and use that in Axis.clear(). Ideally, the same code would be used in Text.__init__() as well so that we don't have duplications in the defaults, but I haven't looked into how difficult that is. If not otherwise possible, it'd be bearable to duplicate the defaults in the reset function.

@tsumli
tsumli force-pushed the bugfix/axis-label branch from e6a280b to 25a7adb Compare August 11, 2022 12:04
@tsumli

tsumli commented Aug 13, 2022

Copy link
Copy Markdown
Contributor Author

Sorry for the late commits and replying. _reset_visual_defaults function is created in Text class and used in clear function so that all attributes are initialized if called clear().

@timhoffm

timhoffm commented Aug 13, 2022 •

Copy link
Copy Markdown
Member

@tsumli Thanks for the update. I’m on holidays right now and cannot properly review for about a week. Will get back to you after that.

@timhoffm timhoffm left a comment

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.

This looks good modulo a tiny naming suggestion.

Comment thread lib/matplotlib/axis.py Outdated
super().__init__(*args, **kwargs)
self._set_default()

def _set_default(self):

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.

Suggested change
def _set_default(self):
def _init(self):
"""
Initialize the label and offsetText instance values and
`label_position` / `offset_text_position`.
"""

I think it's clearer to name this _init because this is really the __init__ logic extracted in a separate method.

Comment thread lib/matplotlib/text.py Outdated
rotation_mode=None,
**kwargs,
):
self._text = ''

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.

Move this line to __init__ (this is just for creating the argument, set_text updates it.

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.

I also thnik that self.update doesn't really belong in _reset...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your review @oscargus ! but I'm a bit comfused about the first comment.
You mean remove _rest_visual_defaults in Text should be removed and the following lines (ll 200-) are moved in __init__ right?

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.

Sorry.

I meant that the lines self._text = '' and self.update(**kwargs) should be moved to __init__,

so something like:

self._text = ''
self._reset_visual_defaults(...)  # without kwargs
self.update(kwargs)

@tsumli

tsumli commented Aug 20, 2022

Copy link
Copy Markdown
Contributor Author

@timhoffm @oscargus thank you for reviewing!
Revised code was pushed and I checked my code runs correctly.

Comment thread lib/matplotlib/text.py Outdated
@@ -165,6 +165,37 @@ def __init__(self,
self._x, self._y = x, y
self._text = ''
self.set_text(text)

@oscargus oscargus Aug 20, 2022 •

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.

Sorry, just

self._text = ''

should be here (to initialize the _text attribute).

self.set_text(text) should still go in _reset_visual_defaults. If not, it will not clear the text contents. (If it works, it is a sign of something else being dodgy.)

@tsumli
tsumli requested a review from oscargus August 21, 2022 10:31
@oscargus

Copy link
Copy Markdown
Member

Thanks and congratulations on your first merged PR! Hope to see you around!

@oscargus
oscargus merged commit c4082c1 into matplotlib:main Aug 21, 2022
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Aug 21, 2022
@tacaswell tacaswell modified the milestones: v3.6.0, v3.7.0 Aug 26, 2022
imgbot Bot pushed a commit to kkipngenokoech/matplotlib that referenced this pull request Apr 19, 2026
…otlib#21253)

* add: _reset_visual_defaults function in Text class

* retrigger checks

* refactor: Axis._init and Text.__init__

* bugfix: reset text to blank string in clear

* refactor: set_text to reset_visual_defaults

* refactor: delete unnecessary set_text in Axis.clear

Co-authored-by: tsumli <sample@example.com>
Co-authored-by: tsumli <example@example.com>
imgbot Bot pushed a commit to kkipngenokoech/matplotlib that referenced this pull request Apr 19, 2026
…otlib#21253)

* add: _reset_visual_defaults function in Text class

* retrigger checks

* refactor: Axis._init and Text.__init__

* bugfix: reset text to blank string in clear

* refactor: set_text to reset_visual_defaults

* refactor: delete unnecessary set_text in Axis.clear

Co-authored-by: tsumli <sample@example.com>
Co-authored-by: tsumli <example@example.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

5 participants