FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

`fontset` from `mathtext` throwing error after setting Text `font=` · Issue #20099 · matplotlib/matplotlib · GitHub

Repository navigation

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

Closed

Description

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

Activity

  1. ain-soph commented on Apr 28, 2021

    Contributor

    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.

  2. diegopetrola commented on Apr 28, 2021

    ContributorAuthor

    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

  3. ain-soph commented on Apr 28, 2021

    Contributor

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

  4. changed the title [-]Error while setting `rcParams['mathtext.fontset']`[/-] [+]`fontset` from `mathtext` throwing error after setting Text `font=`[/+] on Apr 28, 2021
  5. diegopetrola commented on Apr 28, 2021

    ContributorAuthor

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

  6. ain-soph commented on Apr 28, 2021

    Contributor

    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.

  7. ain-soph commented on Apr 28, 2021

    Contributor

    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.

  8. ain-soph commented on Apr 28, 2021

    Contributor
        @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
    
  9. ain-soph commented on Apr 28, 2021

    Contributor

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

  10. ain-soph commented on May 26, 2021

    Contributor

    May close this issue. @diegopetrola

  11. diegopetrola commented on May 26, 2021

    ContributorAuthor

    Congratulations on solving your first issue :)

  12. added this to the v3.5.0 milestone on May 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL