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.
....
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'
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.
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
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).
changed the title [-]Error while setting `rcParams['mathtext.fontset']`[/-][+]`fontset` from `mathtext` throwing error after setting Text `font=`[/+]on 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.
(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.
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
Activity
ain-soph commented on 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.
It works fine if you set math_fontfamily, which is my current workaround.
My expectation is if I don't set math_fontfamily='cm', it should use matplotlib.rcParams['mathtext.fontset'] as default.
diegopetrola commented on Apr 28, 2021
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 commented on 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 commented on 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 commented on 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 commented on Apr 28, 2021
I try to add some debug print
matplotlib/lib/matplotlib/font_manager.py
Line 896 in 54a4b6f
(before the end of get_math_fontfamily) with print(id(self), 'get', self._math_fontfamily)
matplotlib/lib/matplotlib/font_manager.py
Line 924 in 54a4b6f
(after the end of set_math_fontfamily) with print(id(self), 'set', self._math_fontfamily).
Here's the log before 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 commented on Apr 28, 2021
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:
ain-soph commented on Apr 28, 2021
Have found the issue, will submit a PR (very small fix) soon.
ain-soph commented on May 26, 2021
May close this issue. @diegopetrola
diegopetrola commented on May 26, 2021
Congratulations on solving your first issue :)