Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
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. |
diegopetrola commentedAug 3, 2020 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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 afamily
andfontproperties
. Is this the same? Isfontset
just an alias formathtext_fontset
? Why?
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
diegopetrola commentedAug 3, 2020 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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 |
diegopetrola commentedAug 6, 2020 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@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:
|
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
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.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
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.
iffontsetisNoneorfontset=='': | |
iffontsetisNone: |
Is there any precedence of supporting an empty string and that being equal toNone
?
diegopetrolaAug 7, 2020 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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 supportfontset=None is to make it more like the otherrcParams
(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 likeax.title()
andax.xlabel()
would not be correct because the Text object is created before these calls, I would probably need to changeax.title()
,ax.xlabel()
,ax.ylabel()
and probably many others.
By leavingmathtext_fontset = None
I ensure the original behavior is maintained, that is: the texts with nomath_font
will all be rendered with thercParam
present at the momentparse()
is called.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
I wonder if we should go with This would create a naming difference between the |
Uh oh!
There was an error while loading.Please reload this page.
diegopetrola commentedAug 8, 2020 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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 |
Uh oh!
There was an error while loading.Please reload this page.
Shouldn't this |
I do a git log and rebase versus the last commit I didn't author as part of the PR. |
QuLogic commentedAug 15, 2020 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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! :) |
diegopetrola commentedAug 16, 2020 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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. |
diegopetrola commentedAug 17, 2020 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@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. |
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
diegopetrola commentedAug 23, 2020 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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 thismay 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?
diegopetrolaAug 25, 2020 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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 frombaseline_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...
diegopetrolaAug 25, 2020 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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. |
isentropic commentedOct 15, 2020
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 |
isentropic commentedOct 15, 2020
Nevermind please, I see it is under 3.4 milestone. I wonder when can we approximately expect it to be released? |
ain-soph commentedApr 27, 2021 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@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 importmatplotlib.pyplotaspltimportmatplotlibmatplotlib.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. |
Uh oh!
There was an error while loading.Please reload this page.
PR Summary
Closes#17906
PR Checklist