Skip to content

fontset from mathtext throwing error after setting Text font= #20099

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

Closed
diegopetrola opened this issue Apr 28, 2021 · 10 comments
Closed

fontset from mathtext throwing error after setting Text font= #20099

diegopetrola opened this issue Apr 28, 2021 · 10 comments
Milestone

Comments

@diegopetrola
Copy link
Contributor

diegopetrola commented Apr 28, 2021

Bug report

This issue was initially reported by the user @ain-soph at the PR #18145. I was able to reproduce the error with the provided code and decided to formalize the issue here while I work on it.

Code for reproduction:

import matplotlib.pyplot as plt
import matplotlib

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()

And the outcome is:

....
 File "c:\repos\matplotlib\matplotlib\lib\matplotlib\mathtext.py", line 431, in _parse_cached
    else _api.check_getitem(
  File "c:\repos\matplotlib\matplotlib\lib\matplotlib\_api\__init__.py", line 187, in check_getitem
    raise ValueError(
ValueError: None is not a valid value for fontset; supported values are 'cm', 'dejavuserif', 'dejavusans', 'stix', 'stixsans', 'custom'

Matplotlib version: 3.4.1.post563+g679ca172e.d20210428

@ain-soph
Copy link
Contributor

ain-soph commented Apr 28, 2021

Thanks for your quick response.

But I think the title might not describe the issue very well. The current math_fontfamily implementation is irrelevant to rcParams['mathtext.fontset']. A better title might be Error while setting font without setting math_fontfamily

I think the problem is: If you set font manually, you have to set math_fontfamily manually as well.

plt.text(1, 7, msg, size=12, font='Arial')  # math_fontfamily='cm'

It works fine if you set math_fontfamily, which is my current workaround.

plt.text(1, 7, msg, size=12, font='Arial', math_fontfamily='cm')  # good

My expectation is if I don't set math_fontfamily='cm', it should use matplotlib.rcParams['mathtext.fontset'] as default.

@diegopetrola
Copy link
Contributor Author

I was able to determined that the bug stems from the changes done here #18862

If I change to the previews version, the code will work. Unfortunately the PR in question is not very descriptive on the reasons behind this change. Perhaps someone involved at the time can help me? @anntzer

@ain-soph
Copy link
Contributor

ain-soph commented Apr 28, 2021

https://github.com/matplotlib/matplotlib/pull/18862/files#diff-aca801331b4c856bff720af0575b9ad63f402c893abd7d939e5547ba961ee324L900-L902

The removed 2 lines (900 and 901) might be the reason. In my debugging, the fontproperties in the Text instance is Arial with _math_fontfamily=None (while it should not be None).

@diegopetrola diegopetrola changed the title Error while setting rcParams['mathtext.fontset'] fontset from mathtext throwing error after setting Text font= Apr 28, 2021
@diegopetrola
Copy link
Contributor Author

diegopetrola commented Apr 28, 2021

@ain-soph I edited the title to make it more descriptive. I can change it for a better one if you have suggestions.

@ain-soph
Copy link
Contributor

ain-soph commented Apr 28, 2021

So the previous solutions of 3.3 is lazy, which means to save self._math_fontfamily=None at set_math_fontfamily, and change to rcParams['mathtext.fontset'] at get_math_fontfamily.
The author change the behavior to creation time, set self._math_fontfamily=rcParams['mathtext.fontset'] at set_math_fontfamily.
But it seems not always working, sometimes after calling set_math_fontfamily at initialization, self._math_fontfamily is set back to None by some other operations. So it throws exception when check_getitem.

@ain-soph
Copy link
Contributor

ain-soph commented Apr 28, 2021

I try to add some debug print

return self._math_fontfamily

(before the end of get_math_fontfamily) with print(id(self), 'get', self._math_fontfamily)

self._math_fontfamily = fontfamily

(after the end of set_math_fontfamily) with print(id(self), 'set', self._math_fontfamily).

Here's the log before exception:

......
2251559120368 set cm
2251559120560 set cm
2251561657872 set cm
2251561659120 set cm
......
2251563080192 get cm
2251563155856 get cm
2251563157728 get cm
2251563159504 get cm
2251559120560 get None
2251559120560 get None
2251559120560 get None
2251559120560 get None
<Exception>

We can see the error occurs at id 2251559120560 because the _math_fontfamily is still None. But If you search 2251559120560 you'll find its self._math_fontfamily is set to cm previously (at very beginning). So there's some operation that modifies self._math_fontfamily to None in the middle.
Not quite understand why it works well when you pass the argument math_fontfamily='cm'. Hope this information helps.

@ain-soph
Copy link
Contributor

ain-soph commented Apr 28, 2021

    @property
    def _math_fontfamily(self):
        print(id(self), str(self.__math_fontfamily))
        return self.__math_fontfamily

    @_math_fontfamily.setter
    def _math_fontfamily(self, value):
        previous = str(self.__math_fontfamily)
        self.__math_fontfamily = value
        after = str(self.__math_fontfamily)
        print(id(self), f'{previous:10} {after:10}')

I make it a property so that I can get noticed once self._math_fontfamily is being called or set.
However, I can still only see the problematic fontproperties setter called only once with correct value cm.
When it get called by get_math_fontfamily, it is None at that time.
Mysterious, I don't know when it changes value. And how's that possible to change value without called setter function.

Log:

......
1816804568992 None       cm
1816804568992 cm
1816804568992 set cm
......
1816804568992 None
1816804568992 get None
1816804568992 None
1816804568992 None
1816804568992 get None
1816804568992 None
1816804568992 None
1816804568992 get None
1816804568992 None
1816804568992 None
1816804568992 get None
1816804568992 None

@ain-soph
Copy link
Contributor

Have found the issue, will submit a PR (very small fix) soon.

@ain-soph
Copy link
Contributor

May close this issue. @diegopetrola

@diegopetrola
Copy link
Contributor Author

Congratulations on solving your first issue :)

@QuLogic QuLogic added this to the v3.5.0 milestone May 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants