matplotlib 如果在mac 上普通安裝安裝一般中文沒有問題晤斩,
但是matplot 如果安裝在centos 7 并使用anaconda 安裝的 中文一般就有問題
原因: centos7缺少 相應(yīng)的中文字庫
matplotlib 也缺少相應(yīng)的中文字庫
解決方法:
- 每次編碼配置
#coding:utf-8
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] #用來正常顯示中文標(biāo)簽
plt.rcParams['axes.unicode_minus']=False #用來正常顯示負(fù)號
#有中文出現(xiàn)的情況,需要u'內(nèi)容'
需要看看 我在科賽 看到人家明明支持 微軟雅黑炬称,matplotlib 中文正常 seaborn 亂碼
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei'] # 中文字體設(shè)置-黑體
plt.rcParams['axes.unicode_minus'] = False # 解決保存圖像是負(fù)號'-'顯示為方塊的問題
sns.set(font='Microsoft YaHei') # 解決Seaborn中文顯示問題
不過在我這里失敗了括授。。
[2]. 添加字庫文件,配置字庫路徑,刷新 黎茎,并在編碼中配置
import matplotlib
from matplotlib.font_manager import *
matplotlib.rcParams['axes.unicode_minus']=False
font_rc = {'family': 'WenQuanYi Zen Hei Mono',
'weight': 'normal',
'size' : 22}
matplotlib.rc('font', **font_rc)
參考: http://www.reibang.com/p/7b7a3e73ef21
建議自己先解壓好,然后再上傳到服務(wù)器 上
- 配置centos7 安裝字庫 文件 刷新 識別
在 /usr/share/fonts 路徑下創(chuàng)建存放此字體的文件夾yourfontdir当悔,并下載的ttf文件復(fù)制到y(tǒng)ourfontdir中(可以給文件改個英文名工三,方便操作)
我改成了 mullerfont.ttf
sudo -s
cd /usr/share/fonts/
mkdir yifont
cd yifont
#生成字體索引信息. 會顯示字體的font-family
sudo mkfontscale ## 必須有這步Gㄋ帷!
sudo mkfontdir ## 必須有這步<笳! 否則matplot還是識別不到
#更新字體緩存: ## 必須有這步1号稀掸读!
fc-cache
fc-list |grep mullerfont
## /usr/share/fonts/muller/mullerfont.ttf: WenQuanYi Zen Hei Mono:style=Medium
3.找到 matplotlib 的路徑 并修改 其配置文件 ,并刪除matplot 緩存
查看 matplot 支持的字體
from matplotlib.font_manager import FontManager
import subprocess
fm = FontManager()
mat_fonts = set(f.name for f in fm.ttflist)
mat_fonts
import matplotlib
matplotlib.matplotlib_fname()
### '/root/miniconda3/envs/condaEnv/lib/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc'
cd /root/miniconda3/envs/condaEnv/lib/python3.6/site-packages/matplotlib/mpl-data/
vi matplotlibrc
###剩下的比較關(guān)鍵 ,修改成這個樣子宏多,有一個事情我不確定 這個配置文件單個# 好像不是注釋儿惫,##應(yīng)該是注釋
#font.family : sans-serif
font.serif : DejaVu Serif, WenQuanYi Zen Hei Mono, ....##添加WenQuanYi Zen Hei Mono字體聲明
font.sans-serif : DejaVu Sans, WenQuanYi Zen Hei Mono, ....##添加WenQuanYi Zen Hei Mono字體聲明
axes.unicode_minus : False ## 322 line
然后把 新添加的字體copy到matplot的字體文件目錄中,以你實(shí)際目錄為準(zhǔn)
cd /root/miniconda3/envs/condaEnv/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf
cp /usr/share/fonts/muller/mullerfont.ttf ./
并刪除相關(guān)cache
rm -rf ~/.cache/matplotlib
然后再代碼中聲明
import matplotlib
from matplotlib.font_manager import *
matplotlib.rcParams['axes.unicode_minus']=False
font_rc = {'family': 'WenQuanYi Zen Hei Mono',
'weight': 'normal',
'size' : 22}
matplotlib.rc('font', **font_rc)
就可以用了,其實(shí)我個人 認(rèn)為我配置的不算成功
3.參考這個試試
https://blog.csdn.net/dgatiger/article/details/50414549
https://segmentfault.com/a/1190000005144275
matplotlib 在mac conda 也有中文亂碼的問題
1.下載字體
2.查找conda的matplot的路徑
3.將字體ttf 導(dǎo)入到matplot 的字體庫目錄
4.刪除 matplot的緩存
5.修改matplot的配置文件 很重要
6.重啟conda jupyter
https://blog.csdn.net/jsjxiaobing/article/details/78973142
這里所有的操作都是以黑體字體為例
一. 準(zhǔn)備一個中文字體
二. 清除字體緩存
MAC默認(rèn)的緩存目錄
~/.matplotlib/
1
三. 添加字體到 matplotlib 中
- 添加字體文件 matplotlib的默認(rèn)安裝目錄在
/anaconda2/lib/python2.7/site-packages/matplotlib/mpl-data
1
把字體放在相應(yīng)的目錄fonts/ttf中即可 - 編輯配置文件 matplotlibrc
找到 font.sans-serif,默認(rèn)情況下配置如下:font.sans-serif :DejaVu Sans, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
1
修改為:
font.sans-serif : SimHei, DejaVu Sans, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
1
2
其中 SimHei,為我們準(zhǔn)備的中文字體
四重啟 Anaconda
然后在代碼中添加下邊語句
#!/usr/bin/python
# -- coding: UTF-8 --
1
2
重新運(yùn)行代碼即可在圖標(biāo)中顯示中文
作者:楊小兵
來源:CSDN
原文:https://blog.csdn.net/jsjxiaobing/article/details/78973142
版權(quán)聲明:本文為博主原創(chuàng)文章伸但,轉(zhuǎn)載請附上博文鏈接肾请!
- 下載SimHei.ttf字體
http://www.fontpalace.com/font-details/SimHei/)