Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

WARN: more direct warning ticklabels#26401

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
ksunden merged 1 commit intomatplotlib:mainfromjklymak:warn-better-ticklabels
Aug 3, 2023

Conversation

jklymak
Copy link
Member

PR summary

Closes#26398

This now warns with

/Users/jklymak/matplotlib/testit.py:11: UserWarning: set_ticklabels should only be used with set_ticks or a FixedLocator.  ax.set_yticklabels(map(str, ticks))
importnumpyasnpfrommatplotlibimportcmimportmatplotlib.pyplotaspltforadjustin [True]:fig,ax=plt.subplots(figsize=(5,3))data= [33.4,33.5,33.6]ax.plot(data,data)ticks= [32.8,33.,33.2,33.4,33.6,33.8]# ax.set_yticks(ticks)ax.set_yticklabels(map(str,ticks))fig.subplots_adjust(top=0.96)
  • Needs tests

PR checklist

@jklymak
Copy link
MemberAuthor

So the failed tests bring up a use case forset_xticklabels that I don't think our discouragement of the function properly took into account, and that isax.set_xticklabels([]) to stop labelling the ticks, but to keep the actual ticks. The only user-accessible way to do this if we were to get rid ofset_xticklabels would beax.xaxis.set_major_formatter(ticker.NullFormatter()) which I don't think is very user friendly.

This PR checks for an empty list for tick labels, and short circuits to NullFormatter. However, we should probably decide how we want to do this properly. I believe we did this#20047 (ping@timhoffm) and perhaps needs a bit of a rethink.

@jklymakjklymakforce-pushed thewarn-better-ticklabels branch 2 times, most recently from314785e tob0c11c4CompareJuly 30, 2023 05:09
@jklymak
Copy link
MemberAuthor

Flake8 is failing for E721 on a bunch of code I didn't change. I guess a new version of flake8 (doesn't fail locally)

@rcomer
Copy link
Member

I have opened#26414 for thoseflake8 failures.

jklymak reacted with thumbs up emoji

@jklymakjklymak marked this pull request as ready for reviewJuly 30, 2023 15:05
@jklymakjklymakforce-pushed thewarn-better-ticklabels branch fromb0c11c4 todc667deCompareJuly 30, 2023 15:05
if isinstance(locator, mticker.FixedLocator):
if not labels:
# eg labels=[]:
formatter = mticker.NullFormatter()
Copy link
Member

@timhoffmtimhoffmJul 30, 2023
edited
Loading

Choose a reason for hiding this comment

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

This does not have an effect if you return on the next line.formatter is only used in lines 2005ff.

Copy link
Member

Choose a reason for hiding this comment

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

Obviously, this is untested. You can add a test that there is no warning using

with warnings.catch_warnings():    warnings.simplefilter("error")

https://docs.pytest.org/en/7.0.x/how-to/capture-warnings.html#additional-use-cases-of-warnings-in-tests

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Oops thanks I knew there was a reason to not do that.

It is tested, but the old code ends up being used

I'll move to draft as I'm away for a few days

Copy link
Member

Choose a reason for hiding this comment

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

Is the warning filter necessary, since we run with warnings as errors in general? C.f. the test added at#26300.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

It's not necessary but prevents the same warning appearing with two different syntaxes.

rcomer reacted with thumbs up emoji
@timhoffm
Copy link
Member

Since#26414 is in, you can rebase to fix flake8.

locs = self.get_majorticklocs()
ticks = self.get_major_ticks(len(locs))
with warnings.catch_warnings():
warnings.filterwarnings("ignore", message="FixedFormatter should only ")
Copy link
Member

Choose a reason for hiding this comment

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

Let's be more explicit:

Suggested change
warnings.filterwarnings("ignore",message="FixedFormatter should only ")
warnings.filterwarnings(
"ignore",
message="FixedFormatter should only be used together with FixedLocator")

Also, I'm tempted to move this in to be only aroundset_..._formatter

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Repeat the filter twice? Doesn't seem necessary, though it does make it more obvious what call is getting the filter.

if isinstance(locator, mticker.FixedLocator):
if not labels:
# eg labels=[]:
formatter = mticker.NullFormatter()
Copy link
Member

Choose a reason for hiding this comment

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

Obviously, this is untested. You can add a test that there is no warning using

with warnings.catch_warnings():    warnings.simplefilter("error")

https://docs.pytest.org/en/7.0.x/how-to/capture-warnings.html#additional-use-cases-of-warnings-in-tests

@jklymakjklymakforce-pushed thewarn-better-ticklabels branch fromdc667de toc472067CompareAugust 1, 2023 21:07
@tacaswelltacaswell added this to thev3.8.0 milestoneAug 2, 2023
Copy link
Member

@tacaswelltacaswell 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 to me.

I agree with Tim's suggestions, but leaving an approval to expedite merging once fixed.

@jklymak
Copy link
MemberAuthor

jklymak commentedAug 2, 2023
edited
Loading

~Ooops yes I forgot to address those yet. Please let me fix before merging. ~

Should be fine now

@jklymakjklymakforce-pushed thewarn-better-ticklabels branch fromc472067 to871f0e4CompareAugust 3, 2023 15:22
@jklymakjklymakforce-pushed thewarn-better-ticklabels branch from871f0e4 todd2e6f8CompareAugust 3, 2023 16:21
@ksundenksunden merged commit440f761 intomatplotlib:mainAug 3, 2023
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@timhoffmtimhoffmtimhoffm left review comments

@rcomerrcomerrcomer left review comments

@tacaswelltacaswelltacaswell approved these changes

@ksundenksundenksunden approved these changes

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
v3.8.0
Development

Successfully merging this pull request may close these issues.

[Bug]: fig.subplots_adjust and ax.set_yticklabels together can produce unexpected results
5 participants
@jklymak@rcomer@timhoffm@tacaswell@ksunden

[8]ページ先頭

©2009-2025 Movatter.jp