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

简介: 在Deepin20系统下,如何解决Linux系统中matplotlib和seaborn绘图时出现的中文乱码问题,提供了临时和永久的解决方法,包括更换字体设置、修改配置文件和清除缓存等步骤。

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

中文不显示,显示小方框

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,#作用就是解决负号’-'显示为方块的问题

配置效果图下

2.png

3.png

(5)删除缓存
查找缓存目录

import matplotlib    
print(matplotlib.get_cachedir())

删除缓存

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

再次执行代码就不会乱码了
4.png

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

5.png

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

6.png

目录
相关文章
|
2天前
|
JSON JavaScript Linux
Linux系统之安装cook菜谱工具
【10月更文挑战第15天】Linux系统之安装cook菜谱工具
11 2
Linux系统之安装cook菜谱工具
|
4天前
|
Ubuntu Linux 测试技术
Linux系统之Ubuntu安装cockpit管理工具
【10月更文挑战第13天】Linux系统之Ubuntu安装cockpit管理工具
25 4
Linux系统之Ubuntu安装cockpit管理工具
|
3天前
|
安全 Linux
Linux系统之lsof命令的基本使用
【10月更文挑战第14天】Linux系统之lsof命令的基本使用
24 2
Linux系统之lsof命令的基本使用
|
5天前
|
Linux 网络安全 数据安全/隐私保护
Linux系统之Centos7安装cockpit图形管理界面
【10月更文挑战第12天】Linux系统之Centos7安装cockpit图形管理界面
24 1
Linux系统之Centos7安装cockpit图形管理界面
|
4天前
|
Linux
Linux 系统五种帮助命令的使用
Linux 系统五种帮助命令的使用
30 14
|
1天前
|
运维 网络协议 Linux
linux系统命令 losf详解
**lsof命令**(List Open Files)是Linux系统中一个非常实用的工具,用于列出当前系统上所有打开的文件以及与之关联的进程。以下是对lsof命令的详细介绍: ### 一、基本功能 lsof命令可以显示系统中被进程打开的文件,这些文件可以是普通文件、目录、网络套接字、设备文件等。通过lsof命令,用户可以方便地查看哪些文件被哪些进程打开,以及这些文件的状态信息。 ### 二、基本语法 lsof命令的基本语法为:`lsof [选项] [文件]`。其中,选项用于指定lsof命令的行为,文件则是可选的,用于指定要查询的文件。 ### 三、常用选项 * `-a` 或 `-
|
2天前
|
数据可视化 数据挖掘 Python
Matplotlib 教程 之 Seaborn 教程 8
Seaborn 是基于 Matplotlib 的 Python 数据可视化库,专注于统计图形的绘制。它提供了简洁的高级接口和美观的默认样式,支持多种图表类型,如散点图、折线图、柱状图、热图等,特别适合于数据分析和展示。例如,使用 `sns.boxplot()` 可以轻松绘制箱线图,展示数据的分布情况。
11 3
|
1天前
|
数据可视化 Python
Matplotlib 教程 之 Seaborn 教程 10
Seaborn 是基于 Matplotlib 的 Python 数据可视化库,专注于统计图形的绘制。它提供了高级接口和美观的默认主题,简化了复杂图形的生成过程。Seaborn 支持多种图表类型,如散点图、折线图、柱状图、热图等,并特别强调视觉效果。例如,使用 `sns.violinplot()` 可以轻松绘制展示数据分布的小提琴图。
9 1
|
1天前
|
数据可视化 Python
Matplotlib 教程 之 Seaborn 教程 9
Seaborn 是基于 Matplotlib 的 Python 数据可视化库,专注于统计图形的绘制。它提供了高级接口和美观的默认主题,简化了复杂图形的生成过程。本文介绍了 Seaborn 的主要功能和绘图函数,包括热图 `sns.heatmap()` 的使用方法和示例代码。
7 1
|
1天前
|
Linux Shell
Linux系统
是对Linux系统进行管理的命令。对于Linux系统来说,无论是中央处理器、内存、磁盘驱动器、键盘、鼠标,还是用户等都是文件,Linux系统管理的命令是它正常运行的核心,与之前的DOS命令类似。linux命令在系统中有两种类型:内置Shell命令和Linux命令。