-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support customizing antialiasing for text and annotation #25775
Conversation
There was a problem hiding this 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.
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 to https://matplotlib.org/stable/users/explain/backends.html. I have completed this feature in both Agg and Cairo. |
lib/matplotlib/text.py
Outdated
|
||
Parameters | ||
---------- | ||
b : bool or None |
There was a problem hiding this comment.
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 bool or interpret None
as "reset to default". Letting the None
leak through seems like the worst of our options here.
There was a problem hiding this comment.
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 interpret What about don't allow None
as "reset to default".None
as input, like Line2D.set_antialiased()
?
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 at
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 think this is the correct backends that need this. |
Could you add a |
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
I believe we have finished.
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 like |
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also note that antialiasing for coordinate axes will be set with ``rcParams['text.antialiased']`` when they are created and cannot be changed afterwards. | |
Also note that antialiasing for tick labeles will be set with ``rcParams['text.antialiased']`` when they are created and cannot be changed afterwards. |
There was a problem hiding this comment.
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
vs matplotlib.axis.Axis
). I think tick labels (our name for the text on the ticks), see https://matplotlib.org/stable/gallery/showcase/anatomy.html for the very particular names we use for things.
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. |
@stevezhang1999 Would you be willing to squash this to one or two commits? |
aedbdf2
to
8890c34
Compare
@tacaswell Great to talk to you yesterday! Now we have only one commit for this. I also added a one-sentence note under |
test failure seems unrelated. |
This comment was marked as resolved.
This comment was marked as resolved.
702129b
to
98f397a
Compare
@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. |
@stevezhang1999 Could you rebase this again to pick up the CI fix? |
98f397a
to
5e5a03d
Compare
@tacaswell Done! |
Thanks @stevezhang1999 and congratulations to your first merged PR in Matplotlib! Hope to see you around! |
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
how it is done
It's realized by changing how antialiased is set in backends -
draw_text()
inbackend_agg.py
.Now we use
GraphicsContext.get_antialiased()
instead ofrcParams['text.antialiased']
GraphicsContext
object is retrieved and updated byText
andAnnotation
objects whendraw()
is calledlimitations
Currently, it's only supported by Agg and Cairo backend. For all other backends, I didn't see they are checking
rcParams['text.antialiased']
to customize text antialiasing.Note that to set antialiasing for math expressions, the only way is still using
rcParams['text.antialiased']
, as the process is a bit different from normal text (including annotation)notes for tests
Unit tests ensure that getter/setter in
Text
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
PR Checklist
Linked Issue
Documentation and Tests
pytest
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
N/A - no doc added
N/A - docstring already have antialiased, modification is not needed