您当前的位置: 首页 >  linux

Better Bench

暂无认证

  • 1浏览

    0关注

    695博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

【Deepin20系统】Linux系统中永久解决matplotlib画图中文乱码问题和使用seaborn中文乱码问题

Better Bench 发布时间:2021-12-29 17:25:38 ,浏览量:1

目录
  • 1 matplotlib乱码问题
    • 1 问题
    • 1.2 解决
      • 1.2.1 方法一
      • 1.2.2 方法二
  • 2 seaborn中文乱码问题
    • 2.1 问题
    • 2.2 解决

1 matplotlib乱码问题 1 问题
import scipy.stats as st
import pandas as pd 
import matplotlib.pyplot as plt
import seaborn as sns 

x = [1, 2, 3, 4, 5]  
y = [1, 4, 9, 16, 25]  

plt.plot(x, y)  
plt.title('折线图')
plt.xlabel('X值')
plt.ylabel('Y值')
plt.grid()
plt.show()

在这里插入图片描述

中文不显示,显示小方框

1.2 解决 1.2.1 方法一

输入如下代码可以查看系统可用字体

from matplotlib.font_manager import FontManager
fm = FontManager()
mat_fonts = set(f.name for f in fm.ttflist)
print mat_fonts

使用方法

import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']
1.2.2 方法二

永久解决方法,需要在matplotlib包的路径添加字体,并修改配置文件

(1)下载SimHei.ttf字体

http://xiazaiziti.com/210356.html

(2)查找matplotlib路径

import matplotlib
print(matplotlib.get_data_path())  # 数据路径

我的输出是

/anaconda3/envs/tf2/lib/python3.6/site-packages/matplotlib

(3)将字体文件复制到/matplotlib/mpl-data/fonts/ttf中去

cp -f SimHei.ttf /anaconda3/envs/tf2/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf

(4)修改matplotlibrc文件

cd /anaconda3/envs/tf2/lib/python3.6/site-packages/matplotlib/mpl-data

vim matplotlibrc

第一步:输入斜杠/font.family 查找定位到,去掉#注释

font.family:sans-serif

第二步:输入/font.sans-serif 查找定位到,去掉注释,并添加SimHei,

font.sans-serif:SimHei,…

第三步:输入/axes.unicode_minus 查找定位到,去掉注释,将True改为false

axes.unicode_minus:False,#作用就是解决负号’-'显示为方块的问题

配置效果图下

在这里插入图片描述 在这里插入图片描述 (5)删除缓存 查找缓存目录

import matplotlib    
print(matplotlib.get_cachedir())

删除缓存

rm -rf  /Users/..../.matplotlib

再次执行代码就不会乱码了 在这里插入图片描述

2 seaborn中文乱码问题 2.1 问题

当引用seaborn的绘图风格时,还会出现乱码

import scipy.stats as st
import pandas as pd 
import matplotlib.pyplot as plt
import seaborn as sns 

sns.set_style('darkgrid')
x = [1, 2, 3, 4, 5]  
y = [1, 4, 9, 16, 25]  

plt.plot(x, y)  
plt.title('折线图')
plt.xlabel('X值')
plt.ylabel('Y值')
plt.show()

在这里插入图片描述

2.2 解决

指定字体,就可以解决

import pandas as pd 
import matplotlib.pyplot as plt
import seaborn as sns 

sns.set_style('darkgrid', {'font.sans-serif': 'simhei'})
x = [1, 2, 3, 4, 5]  
y = [1, 4, 9, 16, 25]  

plt.plot(x, y)  
plt.title('折线图')
plt.xlabel('X值')
plt.ylabel('Y值')
plt.show()

在这里插入图片描述

关注
打赏
1665674626
查看更多评论
立即登录/注册

微信扫码登录

0.0455s