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

Support customizing antialiasing for text and annotation#25775

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

Conversation

stevezhang1999
Copy link
Contributor

@stevezhang1999stevezhang1999 commentedApr 26, 2023
edited
Loading

PR Summary

Closes#25675

Change overview: now text objects and annotation objects support parameter 'antialiased'.
When antialiased is not specified, it will use the value from rcParams instead.
Examples are

mpl.text.Text(.5, .5, "foo\nbar", antialiased=True)plt.text(0.5, 0.5, '6 inches x 2 inches', antialiased=True)ax.annotate('local max', xy=(2, 1), xytext=(3, 1.5), antialiased=False)

how it is done

It's realized by changing how antialiased is set in backends -draw_text() inbackend_agg.py.
Now we useGraphicsContext.get_antialiased() instead ofrcParams['text.antialiased']
GraphicsContext object is retrieved and updated byText andAnnotation objects whendraw() is called

limitations

Currently, it's only supported by Agg and Cairo backend. For all other backends, I didn't see they are checkingrcParams['text.antialiased'] to customize text antialiasing.

Note that to set antialiasing for math expressions, the only way is still usingrcParams['text.antialiased'], as the process is a bit different from normal text (including annotation)

notes for tests

Unit tests ensure that getter/setter inText andAnnotation objects works well. I didn't include the tests for the whole pipeline because it may require uploading images.

The integration test code is below - we are expected to see texts without antialiasing. The code below is for Agg. If we wnat to test with Cairo, please replace 'TkAgg' with 'GTK3Cairo' in line 14

import matplotlib as mplimport matplotlib.pyplot as pltimport numpy as npimport sysdef is_venv():    return (hasattr(sys, 'real_prefix') or            (hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix))print("Inside venv: ", is_venv())mpl.use('TkAgg')print("backend: ", mpl.rcParams['backend'])print("*"*10, "simple unit test", "*"*10)txt2 = mpl.text.Text(.5, .5, "foo\nbar", antialiased=True)print("txt2.get_antialiased(): ", txt2.get_antialiased())print("*"*35)print("Test: plt.text")mpl.rcParams['text.antialiased'] = Truetext_kwargs = dict(ha='center', va='center', fontsize=28, color='C1', antialiased=False)plt.subplots(figsize=(6, 2))plt.text(0.5, 0.5, '6 inches x 2 inches', **text_kwargs)plt.show()fig, ax = plt.subplots(figsize=(3, 3))t = np.arange(0.0, 5.0, 0.01)s = np.cos(2*np.pi*t)line, = ax.plot(t, s, lw=2)print("Test: ax.annotate")mpl.rcParams['text.antialiased'] = Trueax.annotate('local max', xy=(2, 1), xytext=(3, 1.5),            arrowprops=dict(facecolor='black', shrink=0.05), antialiased=False)ax.set_ylim(-2, 2)plt.show()

PR Checklist

Linked Issue

  • Added "closes #0000" in the PR description to link it to the original issue.

Documentation and Tests

  • Has pytest style unit tests (andpytest passes)
    units tests for new getter and setter for attribute "antialiased"
    integrate tests for run the whole thing, please see the scripts at the end
  • Documentation is sphinx and numpydoc compliant (the docs shouldbuild without error).
    N/A - no doc added
  • New plotting related features are documented with examples.
    N/A - docstring already have antialiased, modification is not needed

Copy link

@github-actionsgithub-actionsbot 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 uson gitter for real-time discussion.

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

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

@tacaswell
Copy link
Member

I'm not sure that anti-aliasing really makes sense for the vector backends (as we are not doing the rasterization the next program down the chain is).

@tacaswelltacaswell added this to thev3.8.0 milestoneApr 27, 2023
@stevezhang1999
Copy link
ContributorAuthor

I'm not sure that anti-aliasing really makes sense for the vector backends (as we are not doing the rasterization the next program down the chain is).

Thanks for the suggestion! Could you talk more about what backends should be supported? Based on my understanding, Agg is raster and Cairo is raster or vector, according tohttps://matplotlib.org/stable/users/explain/backends.html. I have completed this feature in both Agg and Cairo.


Parameters
----------
b : bool or None
Copy link
Member

Choose a reason for hiding this comment

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

we should either cast to boolor interpretNone as "reset to default". Letting theNone leak through seems like the worst of our options here.

Copy link
ContributorAuthor

@stevezhang1999stevezhang1999Apr 27, 2023
edited
Loading

Choose a reason for hiding this comment

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

Thanks for your suggestion.I will make it interpretNone as "reset to default". What about don't allowNone as input, likeLine2D.set_antialiased() ?

@tacaswell
Copy link
Member

This is probably going to require an API change note. Previously you could globally control the anti-aliasing the text by changing the rcparam at draw time, now the value of the rcparam is attached to the text atText creation time. That is

# previously this was a no-op, now it is what workswith rccontext(text.antialiased=False):    fig, ax = plt.subplots()# previously this had an effect, now this is a no-opwith rccontext(text.antialiased=False):    fig.savefig('/tmp/test.png')

I don't see any good way to warn on this, so I think we just have to document it very well. The best work around is probably to make sure that if you are using contexts both the creation and save are in the same context as that will work both forwards and backwards.

@tacaswell
Copy link
Member

I have completed this feature in both Agg and Cairo.

I think this is the correct backends that need this.

@tacaswell
Copy link
Member

Could you add acheck_figures_equal test where one side uses the rcparam and the other side "manually" sets the anti-alisaing state?

@stevezhang1999

This comment was marked as resolved.

@stevezhang1999

This comment was marked as resolved.

@stevezhang1999
Copy link
ContributorAuthor

stevezhang1999 commentedMay 3, 2023
edited
Loading

I believe we have finished.

NowText._antialiased is eitherTrue orFalse. That means, even if user didn't specifyantialiased when they created the figure and text (including the coordinate axes), the user can no longer change it - this will be set bympl.rcParams['text.antialiased'] at the timeText object is initialized.

An alternative solution is to allowText._antialiased to beNone and whenText.get_antialiased() is called,mpl.rcParams['text.antialiased'] will be returned. Doing so will make how users were doing continue working.

For the issue mentioned and crossed out above, we may want to provide a way to customize the antialiasing states for figure or subplot, which is not the job of Text.

We want to make the APIs consistent. Other APIs likeLine2D andPatches will make the antialiasing state fixed at the creation and don't allowNone.

@stevezhang1999
Copy link
ContributorAuthor

This is probably going to require an API change note. Previously you could globally control the anti-aliasing the text by changing the rcparam at draw time, now the value of the rcparam is attached to the text atText creation time. That is

# previously this was a no-op, now it is what workswith rccontext(text.antialiased=False):    fig, ax = plt.subplots()# previously this had an effect, now this is a no-opwith rccontext(text.antialiased=False):    fig.savefig('/tmp/test.png')

I don't see any good way to warn on this, so I think we just have to document it very well. The best work around is probably to make sure that if you are using contexts both the creation and save are in the same context as that will work both forwards and backwards.

I include this under /api_changes


plt.text(0.5, 0.25, r"$I'm \sqrt{x}$", antialiased=False)

Also note that antialiasing for coordinate axes will be set with ``rcParams['text.antialiased']`` when they are created and cannot be changed afterwards.
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
Also note that antialiasing forcoordinate axes will be set with ``rcParams['text.antialiased']`` when they are created and cannot be changed afterwards.
Also note that antialiasing fortick labeles will be set with ``rcParams['text.antialiased']`` when they are created and cannot be changed afterwards.

Copy link
Member

Choose a reason for hiding this comment

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

Axes vs axis have very specific meaning in Matplotlib (matplotlib.axes.Axes vsmatplotlib.axis.Axis). I think tick labels (our name for the text on the ticks), seehttps://matplotlib.org/stable/gallery/showcase/anatomy.html for the very particular names we use for things.

@tacaswell
Copy link
Member

Good to talk to you on the call today and I apologize for being slow to review!

I think this is getting very close, just the finishing details on the docs.

@tacaswell
Copy link
Member

@stevezhang1999 Would you be willing to squash this to one or two commits?

@stevezhang1999
Copy link
ContributorAuthor

stevezhang1999 commentedMay 26, 2023
edited
Loading

@stevezhang1999 Would you be willing to squash this to one or two commits?

@tacaswell Great to talk to you yesterday! Now we have only one commit for this.

I also added a one-sentence note underText.set_antialiased, for the limitation that we cannot customizing antialiasing with mathtext with parameterantialiased. As far as I'm concerned, we may need to modify lots of classes (includingText,mathtext, backends and more) to makeantialiased have an effect - so maybe I (as well as other contributors) can investigate this separately later.

@anntzer
Copy link
Contributor

test failure seems unrelated.

@stevezhang1999

This comment was marked as resolved.

@stevezhang1999
Copy link
ContributorAuthor

@tacaswell would like to know if this is ready to be merged since we have two approvals or there is other things we should look at? This could help me manage the git things for the work built on top of this PR.

@tacaswell
Copy link
Member

@stevezhang1999 Could you rebase this again to pick up the CI fix?

@stevezhang1999
Copy link
ContributorAuthor

@stevezhang1999 Could you rebase this again to pick up the CI fix?

@tacaswell Done!

@oscargus
Copy link
Member

Thanks@stevezhang1999 and congratulations to your first merged PR in Matplotlib! Hope to see you around!

@oscargusoscargus merged commit74c0cd0 intomatplotlib:mainJun 2, 2023
@stevezhang1999stevezhang1999 mentioned this pull requestJun 8, 2023
3 tasks
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@github-actionsgithub-actions[bot]github-actions[bot] left review comments

@tacaswelltacaswelltacaswell approved these changes

@anntzeranntzeranntzer approved these changes

@oscargusoscargusAwaiting requested review from oscargus

Assignees
No one assigned
Projects
Milestone
v3.8.0
Development

Successfully merging this pull request may close these issues.

[ENH]: Add get/set_antialiased to Text objects
5 participants
@stevezhang1999@tacaswell@anntzer@oscargus@melissawm

[8]ページ先頭

©2009-2025 Movatter.jp