8000 Fix: axis, ticks are set to defaults fontsize after ax.clear() by tsumli · Pull Request #21253 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

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

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

Merged
merged 6 commits into from
Aug 21, 2022

Conversation

tsumli
Copy link
Contributor
@tsumli tsumli commented Oct 1, 2021

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

Copy link
@github-actions github-actions bot left a comment

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.

@@ -802,6 +802,7 @@ def clear(self):
"""

self.label.set_text('') # self.set_label_text would change isDefault_
self.label.set_fontsize(mpl.rcParams['font.size'])
Copy link
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.

8000 Copy link
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
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
Copy link
Member
jklymak commented Nov 22, 2021

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

@tsumli
Copy link
Contributor Author
tsumli commented Nov 22, 2021

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
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
Copy link
Member
jklymak commented Feb 1, 2022

@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
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
Copy link
Contributor Author
tsumli commented Aug 13, 2022

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
Copy link
Member
timhoffm commented Aug 13, 2022

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

Copy link
Member
@timhoffm timhoffm left a comment

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.

8000

@@ -2193,6 +2197,9 @@ class XAxis(Axis):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._set_default()

def _set_default(self):
Copy link
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.

linespacing=None,
rotation_mode=None,
**kwargs,
):
self._text = ''
Copy link
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
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
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
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
Copy link
Contributor Author
tsumli commented Aug 20, 2022

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

@@ -165,6 +165,37 @@ def __init__(self,
self._x, self._y = x, y
self._text = ''
self.set_text(text)
Copy link
Member
@oscargus oscargus Aug 20, 2022

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
Member

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

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
0