使用matplotlib时missing from current font
时间: 2024-01-07 15:02:32 浏览: 459
这个问题通常是由于 matplotlib 找不到所需字体文件引起的。您可以通过以下几种方法来解决这个问题:
1. 安装所需字体:您需要安装 matplotlib 所需的字体。您可以通过以下命令在 Ubuntu 上安装常用字体:
```
sudo apt-get install fonts-dejavu-core fonts-dejavu-extra fonts-droid-fallback fonts-f500 fonts-fanwood fonts-freefont-ttf fonts-liberation fonts-lmodern fonts-lyx fonts-sil-gentium fonts-texgyre fonts-tlwg-purisa
```
如果您使用的是其他操作系统,请查看其文档以了解如何安装所需字体。
2. 指定字体路径:您可以通过在 matplotlib 中指定字体路径来解决此问题。您可以使用以下代码:
```python
import matplotlib
matplotlib.rcParams['font.family'] = 'sans-serif'
matplotlib.rcParams['font.sans-serif'] = ['DejaVu Sans'] # 指定字体
```
在 `font.sans-serif` 中指定您想要使用的字体即可。
希望这些方法能够解决您的问题。
相关问题
使用matplotlib时UserWarning: Glyph 26412 (\N{CJK UNIFIED IDEOGRAPH-672C}) missing from current font. func(*args)
这个警告是因为你的字体库缺少了显示汉字的字体。你可以通过以下两种方式来解决这个问题:
1. 安装一个包含中文字体的字体库,如SimSun、WenQuanYi等。
2. 在绘制图形时,手动指定一个包含中文字体的字体文件。例如:
```python
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"C:\Windows\Fonts\simhei.ttf") # 指定中文字体文件的路径
plt.title("标题", fontproperties=font)
plt.xlabel("x轴标签", fontproperties=font)
plt.ylabel("y轴标签", fontproperties=font)
plt.show()
```
在这个例子中,我们指定了宋体字体文件的路径,并在绘图时使用了该字体。你需要将 `fname` 参数指定为你所使用的中文字体文件的路径。
E:\visualization\analysis\1.py:80: MatplotlibDeprecationWarning: Support for FigureCanvases without a required_interactive_framework attribute was deprecated in Matplotlib 3.6 and will be removed two minor releases later. plt.show() C:\Program Files\JetBrains\PyCharm 2021.3\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:68: UserWarning: Glyph 21508 (\N{CJK UNIFIED IDEOGRAPH-5404}) missing from current font. FigureCanvasAgg.draw(self) C:\Program Files\JetBrains\PyCharm 2021.3\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:68: UserWarning: Glyph 20010 (\N{CJK UNIFIED IDEOGRAPH-4E2A}) missing from current font. FigureCanvasAgg.draw(self) C:\Program Files\JetBrains\PyCharm 2021.3\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:68: UserWarning: Glyph 26376 (\N{CJK UNIFIED IDEOGRAPH-6708}) missing from current font. FigureCanvasAgg.draw(self) C:\Program Files\JetBrains\PyCharm 2021.3\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:68: UserWarning: Glyph 20221 (\N{CJK UNIFIED IDEOGRAPH-4EFD}) missing from current font. FigureCanvasAgg.draw(self) C:\Program Files\JetBrains\PyCharm 2021.3\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:68: UserWarning: Glyph 30340 (\N{CJK UNIFIED IDEOGRAPH-7684}) missing from current font. FigureCanvasAgg.draw(self) C:\Program Files\JetBrains\PyCharm 2021.3\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:68: UserWarning: Glyph 38144 (\N{CJK UNIFIED IDEOGRAPH-9500}) missing from current font. FigureCanvasAgg.draw(self) C:\Program Files\JetBrains\PyCharm 2021.3\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:68: UserWarning: Glyph 21806 (\N{CJK UNIFIED IDEOGRAPH-552E}) missing from current font. FigureCanvasAgg.draw(self) C:\Program Files\JetBrains\PyCharm 2021.3\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:68: UserWarning: Glyph 24773 (\N{CJK UNIFIED IDEOGRAPH-60C5}) missing from current font. FigureCanvasAgg.draw(self) C:\Program Files\JetBrains\PyCharm 2021.3\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:68: UserWarning: Glyph 20917 (\N{CJK UNIFIED IDEOGRAPH-51B5}) missing from current font. FigureCanvasAgg.draw(self) C:\Program Files\JetBrains\PyCharm 2021.3\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:68: UserWarning: Glyph 36135 (\N{CJK UNIFIED IDEOGRAPH-8D27}) missing from current font. FigureCanvasAgg.draw(self) C:\Program Files\JetBrains\PyCharm 2021.3\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:68: UserWarning: Glyph 21697 (\N{CJK UNIFIED IDEOGRAPH-54C1}) missing from current font. FigureCanvasAgg.draw(self) Process finished with exit code 0
这是一些Matplotlib的警告信息。其中第一条警告表示,Matplotlib 3.6版本开始不再支持没有“required_interactive_framework”属性的FigureCanvases,这个属性用于指定绘图所需的交互式框架。如果你的代码中使用了这样的FigureCanvas,建议更新Matplotlib版本或修改代码以使用最新的FigureCanvas。
其他的警告则是指出当前字体中缺少一些中文字符,这可能会影响绘图结果的显示效果。可以通过更换字体或安装支持中文字符的字体来解决这个问题。
这些警告信息并不会影响代码的运行,只是提醒你可能需要注意一些绘图的细节问题。建议在代码中加入以下语句来禁止警告的输出:
```
import warnings
warnings.filterwarnings("ignore")
```
阅读全文