-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Created a parameter fontset that can be used in each Text element #18145
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
Conversation
The PR needs a bit of the usual polishing (which I'll let others walk you through ;-)), but I think attaching that info to the FontProperties object (rather than as a separate attribute on the Text) is a really nice idea that makes things much simpler than I would have feared -- from a quick look, I think the basic approach is good. |
All the tests that I could do have been done and passed =D |
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 the PR.
I've not read the associated issue. After reading this PR, with particular emphasis on the docs, I have no firm idea what this new kwarg is doing. What happens if I don't set this kwarg? What even is a "fontset"? In the rest of the library we refer to a family
and fontproperties
. Is this the same? Is fontset
just an alias for mathtext_fontset
? Why?
examples/text_labels_and_annotations/mathtext_fontset_example.py
Outdated
Show resolved
Hide resolved
If you dont set this kwarg the mathtext will be rendered with the value specified in
I might be just as lost as you, but I remeber reading somewhere in the docs that a "fontset" is a family of fonts. I will check again to be sure.
I just made this for convenience, there are a lot of aliases for the parameters to create a Text like |
@DiegoLeal They were more rhetorical questions than suggestions that you change anything, though we may want to do so. If I understand correctly then I would explain it like this: Matplotlib has a built in math formatter that uses :rc: I think the todos are:
BTW, please feel free to ping me for a re-review. PRs sometimes get buried, and politely asking for us to take a look again is encouraged (within reason ;-) |
@DiegoLeal Sorry if I was unclear above - I was only suggesting |
Ups! =D |
@jklymak I believe the code is in a good state for a review, please take a look when you can :) Here's a quick preview of what I did:
|
examples/text_labels_and_annotations/mathtext_fontset_example.py
Outdated
Show resolved
Hide resolved
examples/text_labels_and_annotations/mathtext_fontset_example.py
Outdated
Show resolved
Hide resolved
examples/text_labels_and_annotations/mathtext_fontset_example.py
Outdated
Show resolved
Hide resolved
examples/text_labels_and_annotations/mathtext_fontset_example.py
Outdated
Show resolved
Hide resolved
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.
Looks close, but quite a few rough edges to get through. Thanks for your patience!
@@ -364,3 +364,58 @@ def test_mathtext_to_png(tmpdir): | |||
mt = mathtext.MathTextParser('bitmap') | |||
mt.to_png(str(tmpdir.join('example.png')), '$x^2$') | |||
mt.to_png(io.BytesIO(), '$x^2$') | |||
|
|||
|
|||
def test_mathtext_fontset(): |
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.
A few seconds? Thats far too long.
I think we need to add an image (just one please) to the repo for this. Then just use our normal figure comparison machinery. If you are going to just compare if two files are equal you can just save to bytesIO instead of to disk. But in this case I think we need the file comparison.
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.
Mostly stylistic comments.
lib/matplotlib/font_manager.py
Outdated
-------- | ||
.text.Text.set_mathtext_fontset | ||
""" | ||
if fontset is None or fontset == '': |
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.
if fontset is None or fontset == '': | |
if fontset is None: |
Is there any precedence of supporting an empty string and that being equal to 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.
The reason to support fontset=None is to make it more like the other rcParams
(and to avoid some bugs), for instance, if I did this:
plt.rcParams['mathtext.fontset'] = 'dejavusans'
plt.text('text1')
plt.rcParams['mathtext.fontset'] = 'dejavuserif'
plt.text('text2')
The texts would have different fonts. I actually liked that change but the initial behavior was not like that. Also, the the interaction with functions like ax.title()
and ax.xlabel()
would not be correct because the Text object is created before these calls, I would probably need to change ax.title()
, ax.xlabel()
, ax.ylabel()
and probably many others.
By leaving mathtext_fontset = None
I ensure the original behavior is maintained, that is: the texts with no math_font
will all be rendered with the rcParam
present at the moment parse()
is called.
examples/text_labels_and_annotations/mathtext_fontset_example.py
Outdated
Show resolved
Hide resolved
I wonder if we should go with This would create a naming difference between the |
examples/text_labels_and_annotations/mathtext_fontset_example.py
Outdated
Show resolved
Hide resolved
I think this trade off is worth. I will wait some time before making this changes to give people time for a response and avoid having to rename the files again in case we decide for another name. Also, thanks everyone for your reviews and your time I hope my changes, albeit simple, make |
examples/text_labels_and_annotations/mathtext_fontset_example.py
Outdated
Show resolved
Hide resolved
Shouldn't this |
I do a git log and rebase versus the last commit I didn't author as part of the PR. |
It looks like you squashed some commits, but didn't do a rebase (check To reset to the squashed version of commits: $ git checkout issue#17906
$ git reset --hard 682a84866774a168c9d5edcfc64a7f44f58c5fa7 then rebase to current: $ git fetch upstream
$ git rebase -i upstream/master
# change squashing, if you want, but it's already done from the first time you did it You will get stuck somewhere in between because of the conflicts: $ $EDITOR lib/matplotlib/mathtext.py
# fix the conflict here, which has to do with a parameter rename in `_parse_cached`
$ git add lib/matplotlib/mathtext.py
$ git rebase --continue then force push away the merge, and the other set of commits: $ git push --force origin issue#17906 |
I think I finally understand (at least part of it), thank you so much @timhoffm and @QuLogic , after countless hours reading docs and tutorials and trying without success to make a repo for testing I was starting to lose hope, I can not thank you enough. I feel reinvigorated to read a bit more about this stuff now, maybe git is not all the bad things I called it in my thoughts. Thank you very much! :) |
It seems some pytest errors appeared after I solved the conflict with I started investigating this but have yet to finish. |
As I suspected, the errors seems to persist even after I reset my changes (I verified and the errors are the same). |
You can ignore that macOS failure. |
@DiegoLeal sorry this is such a pain! OTOH, I think you have an empty commit - i.e. 0 files changed now. |
@dopplershift thanks for the info! I've put everything in a single commit because I see that's the way it seems to be done by others, I left messages for the big implementations and deleted messages related to debugs and discussion. |
Done and done! I also changed the font a little on the tests to make them more distinguishable. The size of the of the baseline image fell from 8 to 5kb, excellent suggestion QuLogic! |
fig = plt.figure(figsize=(10, 3)) | ||
fig.text(0.2, 0.7, r"$\mathit{This\ text\ should\ have\ one\ font} $", | ||
size=24, math_fontfamily='dejavusans') | ||
fig.text(0.2, 0.3, r"$\mathit{This\ text\ should\ have\ another} $", |
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.
I was about to approve, but what the heck is going on with the baseline in the actual image for this font?
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.
Math text is italic by default, right? While implementing my changes I tested different font styles (including the redundant \mathit{}
) to be sure they were working properly and I must have forgotten to remove this part. My apologies :)
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.
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.
I will look onto that, thx for pointing it out.
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.
I haven't looked carefully but this may just be a very old bug, hinted at in #5414 (comment) (the "wiggly" baselines). IIRC I have a fix somewhere for that, but as usual it never made it even to the PR stage because of course that involves updating all baselines...
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.
I don't think its your changes. OTOH, does all our math look like this or is it just the fact you are calling a sentence?
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.
Yes, it can occur with normal math, but it is only noticeble when there are a lot of letters together, here is one example from baseline_images
:
The probable reason there is not a lot of complaints about this is because it's hard to reproduce, if you put on an axis the wiggling will vanish:
If you try to reproduce the same code outside the test environment, no wiggling will occur either:
It might be some setup done by the testing environment, but that's all the info I gathered so far.
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.
So, can we just add the axes? I don't mean to drag you into a really old bug...
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.
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.
Our tests use weird font settings in order to avoid regenerating images. Hopefully that can be dropped soon.
- The parameter can change the family of fonts for each text element instead of globally with `rcParams['mathtext.fontset']` - Added example of usage `/examples/text_labels_and_annotations/mathtext_fontfamily_example.py` - Added tests - Added documentation
The reason for this is to avoid a bug that occurs in testing where the math text will 'wiggle'. This is an old bug in matplotlib not related to the changes done in this PR.
Thanks for all the hard work @diegopetrola |
Thanks for all your reviews and guidance @jklymak. You and all the other reviewers who participated here. It was a pleasure. |
Hello, I'm the author of the feature request, and I thank the @jklymak and all for making this PR happen. However, under matplotlib 3.3.2
I cannot run the example
errors saying that |
Nevermind please, I see it is under 3.4 milestone. I wonder when can we approximately expect it to be released? |
@diegopetrola If I set the font in text explicitly, then I have to set Here is a simple reproduce snippet: (it would be working well if you pass the argument import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['mathtext.fontset'] = 'cm'
plt.figure(figsize=(6, 5))
plt.plot(range(11), color="0.9")
msg = (r"Normal Text. $Text\ in\ math\ mode:\ "
r"\int_{0}^{\infty } x^2 dx$")
plt.text(1, 7, msg, size=12, font='Arial') # math_fontfamily='cm'
plt.show() I check the error and it seems that, the Error log:
|
Please open a separate issue if this is a problem in the current release. |
PR Summary
Closes #17906
PR Checklist