본문 바로가기
프로그램

[파이썬] matplotlib의 math_fontfamily 에러

by 오디세이99 2022. 8. 13.
728x90
반응형

다음과 같이 plot에서 math_fontfamily 를 사용하니 에러가 발생합니다.

ax.text(1, 7, msg, size=12, math_fontfamily='cm')

 

 

Math fontfamily — Matplotlib 3.5.3 documentation

 

Math fontfamily — Matplotlib 3.5.3 documentation

Note Click here to download the full example code

matplotlib.org

import matplotlib.pyplot as plt


fig, ax = plt.subplots(figsize=(6, 5))

# A simple plot for the background.
ax.plot(range(11), color="0.9")

# A text mixing normal text and math text.
msg = (r"Normal Text. $Text\ in\ math\ mode:\ "
       r"\int_{0}^{\infty } x^2 dx$")

# Set the text in the plot.
ax.text(1, 7, msg, size=12, math_fontfamily='cm')

# Set another font for the next text.
ax.text(1, 3, msg, size=12, math_fontfamily='dejavuserif')

# *math_fontfamily* can be used in most places where there is text,
# like in the title:
ax.set_title(r"$Title\ in\ math\ mode:\ \int_{0}^{\infty } x^2 dx$", math_fontfamily='stixsans', size=14)

# Note that the normal text is not changed by *math_fontfamily*.
plt.show()

 

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-26-2aef8728e5ae> in <module>
     13 
     14 # Set the text in the plot.
---> 15 ax.text(1, 7, msg, size=12, math_fontfamily='cm')
     16 
     17 # Set another font for the next text.

E:\PGM\Anaconda3\lib\site-packages\matplotlib\cbook\deprecation.py in wrapper(*args, **kwargs)
    356                 f"%(removal)s.  If any parameter follows {name!r}, they "
    357                 f"should be pass as keyword, not positionally.")
--> 358         return func(*args, **kwargs)
    359 
    360     return wrapper

E:\PGM\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in text(self, x, y, s, fontdict, withdash, **kwargs)

E:\PGM\Anaconda3\lib\site-packages\matplotlib\text.py in update(self, kwargs)
    175         sentinel = object()  # bbox can be None, so use another sentinel.
    176         bbox = kwargs.pop("bbox", sentinel)
--> 177         super().update(kwargs)
    178         if bbox is not sentinel:
    179             self.set_bbox(bbox)

E:\PGM\Anaconda3\lib\site-packages\matplotlib\artist.py in update(self, props)

E:\PGM\Anaconda3\lib\site-packages\matplotlib\artist.py in <listcomp>(.0)

E:\PGM\Anaconda3\lib\site-packages\matplotlib\artist.py in _update_property(self, k, v)

AttributeError: 'Text' object has no property 'math_fontfamily'

 

 

 

확인해 보니 math_fontfamily는 matplotlib 의 버전 Matplotlib 3.5.3 에서 동작이 됩니다.

제 PC에 설치된 버전은 3.5.2 였습니다.

 

다음과 같이 matplotlib를 upgrade 해주니 설치가 되지 않고 코드도 동일한 에러가 발생합니다.

pip install --upgrade matplotlib

 

--user 옵션을 추가해서 update 하니 설치가 완료되었습니다.

pip install --user --upgrade matplotlib

 

 

Jupyter=Notebook에서 테스트하고 있는데, 기존에 에러가 발생하는 Jupyter-notebook 창에서는 update 한 것이 반영되지 않고 에러가 발생합니다. 창을 다시 열고 테스트하니 에러 없이 실행 됩니다.

 

import matplotlib.pyplot as plt


fig, ax = plt.subplots(figsize=(6, 5))

# A simple plot for the background.
ax.plot(range(11), color="0.9")

# A text mixing normal text and math text.
msg = (r"Normal Text. $Text\ in\ math\ mode:\ "
       r"\int_{0}^{\infty } x^2 dx$")

# Set the text in the plot.
ax.text(1, 7, msg, size=12, math_fontfamily='cm')

# Set another font for the next text.
ax.text(1, 3, msg, size=12, math_fontfamily='dejavuserif')

# *math_fontfamily* can be used in most places where there is text,
# like in the title:
ax.set_title(r"$Title\ in\ math\ mode:\ \int_{0}^{\infty } x^2 dx$", math_fontfamily='stixsans', size=14)

# Note that the normal text is not changed by *math_fontfamily*.
plt.show()

 

728x90
반응형

댓글