fig.canvas.print_figure(bytes_io, **kw) findfont: Font family 'Arial Unicode MS' not found. findfont: Font family 'Arial Unicode MS' not found. findfont: Font family 'Arial Unicode MS' not found.
时间: 2024-02-22 12:00:19 浏览: 26
如果您的系统中没有安装Arial Unicode MS字体,可以尝试使用其他的中文字体,比如微软雅黑、SimHei等。
以微软雅黑为例,您可以将代码修改为:
```python
plt.rcParams["font.family"] = "Microsoft YaHei"
```
然后在需要添加中文的地方使用`fontproperties`参数,比如:
```python
plt.xlabel("占比", fontproperties="Microsoft YaHei", size=12)
plt.ylabel("涨幅", fontproperties="Microsoft YaHei", size=12)
plt.title("股价上涨的公司涨幅分布", fontproperties="Microsoft YaHei", size=14)
```
如果您的系统中也没有安装微软雅黑字体,可以尝试其他中文字体。
相关问题
fig.canvas.print_figure(bytes_io, **kw)
b'fig.canvas.print_figure(bytes_io, **kw)'是一个Matplotlib中的函数,用于将图形保存到字节流中。它接受一个字节流(bytes_io)和其他参数(**kw),将图形保存到该字节流中。通过这个函数,可以将Matplotlib中的图形保存到内存中,方便在网络端进行传输或进行进一步的处理。
报错C:\Anaconda3\lib\site-packages\IPython\core\pylabtools.py:151: UserWarning: Glyph 26032 (\N{CJK UNIFIED IDEOGRAPH-65B0}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) C:\Anaconda3\lib\site-packages\IPython\core\pylabtools.py:151: UserWarning: Glyph 20215 (\N{CJK UNIFIED IDEOGRAPH-4EF7}) missing from current font. fig.canvas.print_figure(bytes_io, **kw)
这是因为默认字体中没有包含某些需要使用的中文字符,导致绘图出现了问题。我们可以在绘图之前设置字体,以解决这个问题。
可以使用如下代码设置字体:
```python
from matplotlib.font_manager import FontProperties
# 设置中文字体
font = FontProperties(fname=r"C:\Windows\Fonts\simhei.ttf", size=14)
```
其中,`fname`是字体文件所在的路径,这里使用的是SimHei字体,您可以根据自己的需要修改。
然后在绘图时,将需要使用中文的地方,比如x轴标签、y轴标签、标题等,都加上`fontproperties=font`的参数,指定使用刚刚设置的字体。
代码如下:
```python
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
# 创建DataFrame对象
data = {'证券代码': ['000609', '000993', '002615', '000795', '002766', '000971', '000633', '300173', '300279', '000831'],
'证券简称': ['中迪投资', '闽东电力', '哈尔斯', '英洛华', '索菱股份', '高升控投', '合金投资', '智慧松德', '和晶科技', '五矿稀土'],
'最新价': [4.8, 4.8, 5.02, 3.93, 6.78, 3.72, 4.6, 4.6, 5.81, 9.87],
'涨跌幅%': [10.09, 10.09, 10.09, 10.08, 10.06, 10.06, 10.06, 10.5, 10.05, 10.04]}
df = pd.DataFrame(data)
# 设置中文字体
font = FontProperties(fname=r"C:\Windows\Fonts\simhei.ttf", size=14)
# 绘制条形图
plt.bar(df['证券简称'], df['最新价'])
plt.xticks(rotation=90, fontproperties=font) # x轴标签旋转90度,防止重叠
plt.xlabel('证券简称', fontproperties=font)
plt.ylabel('最新价', fontproperties=font)
plt.title('股票数据', fontproperties=font)
plt.show()
```
这样就可以成功绘制中文字符了。