mac在matplotlib中顯示中文的操作方法
Matplotlib 是一個 Python 的 2D繪圖庫,它以各種硬拷貝格式和跨平臺的交互式環(huán)境生成出版質(zhì)量級別的圖形 。
通過 Matplotlib,開發(fā)者可以僅需要幾行代碼,便可以生成繪圖,直方圖,功率譜,條形圖,錯誤圖,散點圖等。
下面開始今天的正文。
首先保證電腦里是否安裝了中文字體,然后找到他們!!
具體步驟如下:
先打開終端,command+空格 搜索 ter,然后會蹦出終端,點開
輸入 fc-list :lang=zh
如果顯示command not found
輸入 conda install fontconfig
然后輸入 y
然后就安裝好了!?。?!
再輸入fc-list :lang=zh
好了,看看有哪些字體
剩下就是anaconda的操作了
import matplotlib.pyplot as plt plt.rcParams["font.family"] = 'Arial Unicode MS' import matplotlib a=sorted([f.name for f in matplotlib.font_manager.fontManager.ttflist]) for i in a: print i

#保證中文正常使用 from sklearn.datasets import make_blobs, load_iris import matplotlib.pyplot as plt # 支持中文 plt.rcParams['font.sans-serif'] = ['Arial Black'] # 用來正常顯示中文標(biāo)簽 plt.rcParams['axes.unicode_minus'] = False # 用來正常顯示負(fù)號 n_samples = 1000 random_state = 37 #隨機分割測試集和訓(xùn)練集 x, y = make_blobs(n_samples=n_samples, random_state=random_state) # x, y = load_iris(True) # 鶯尾花 print(x.shape, y.shape) plt.scatter(x[:, 0], x[:, 1], c=y) plt.title(u"原始數(shù)據(jù)分布") plt.xlabel(u"長度") plt.ylabel(u"寬度") plt.show()

知識點補充:
給大家補充一個matplotlib中文亂碼問題
在ubuntu16.04中使用python的matplotlib模塊進(jìn)行科學(xué)制圖時,在輸出圖例或者標(biāo)題的時候出現(xiàn)中文亂碼問題:
解決:
下載字體:msyh.ttf (微軟雅黑),放在系統(tǒng)字體文件夾下: /usr/share/fonts
同時我也復(fù)制了下放在matplotlib的字體文件夾下了(不知道這一步是不是必須)
/usr/local/lib/python3.5/dist-packages/matplotlib/mpl-data/fonts/ttf/
修改matplotlib配置文件:
sudo vim /usr/local/lib/python3.5/dist-packages/matplotlib/mpl-data/matplotlibrc
刪除font.family和font.sans-serif兩行前的#,并在font.sans-serif后添加中文字體
Microsoft YaHei, ...(其余不變)
刪除~/.cache/matplotlib下文件fontList.py3k.cache
重啟python即可
注意:在我修改完成后還需要在代碼里加入:
import maplotlib.pyplot as plt plt.rcParams['font.sans-serif']=['SimHei'] # 顯示中文不亂碼 plt.rcParams['axes.unicode_minus'] = False # 顯示負(fù)數(shù)不亂碼
另外:可以執(zhí)行下這段程序--可以打印出可用的字體:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from matplotlib.font_manager import FontManager
import subprocess
fm = FontManager()
mat_fonts = set(f.name for f in fm.ttflist)
#print(mat_fonts)
output = subprocess.check_output('fc-list :lang=zh -f "%{family}\n"', shell=True)
#print( '*' * 10, '系統(tǒng)可用的中文字體', '*' * 10)
#print (output)
zh_fonts = set(f.split(',', 1)[0] for f in output.decode('utf-8').split('\n'))
available = mat_fonts & zh_fonts
print ('*' * 10, '可用的字體', '*' * 10)
for f in available:
print (f)
總結(jié)
到此這篇關(guān)于mac如何在matplotlib中顯示中文的文章就介紹到這了,更多相關(guān)mac matplotlib 中文內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python實現(xiàn)matplotlib顯示中文的方法詳解
- Python使用Matplotlib模塊時坐標(biāo)軸標(biāo)題中文及各種特殊符號顯示方法
- Matplotlib中文亂碼的3種解決方案
- Python使用matplotlib繪圖無法顯示中文問題的解決方法
- Python在Matplotlib圖中顯示中文字體的操作方法
- Python中matplotlib中文亂碼解決辦法
- ubuntu系統(tǒng)下matplotlib中文亂碼問題的解決方法
- python設(shè)置 matplotlib 正確顯示中文的四種方式
- 完美解決Pycharm中matplotlib畫圖中文亂碼問題
- Python利用Matplotlib繪圖無法顯示中文字體的解決方案
相關(guān)文章
Pandas DataFrame中的tuple元素遍歷的實現(xiàn)
這篇文章主要介紹了Pandas DataFrame中的tuple元素遍歷的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Python GUI學(xué)習(xí)之登錄系統(tǒng)界面篇
這篇文章主要介紹了Python GUI學(xué)習(xí)之登錄系統(tǒng)界面篇,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
利用python3 的pygame模塊實現(xiàn)塔防游戲
這篇文章主要介紹了利用python3 的pygame模塊實現(xiàn)塔防游戲,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-12-12

