mac在matplotlib中如何顯示中文-創(chuàng)新互聯(lián)

成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),東營(yíng)區(qū)企業(yè)網(wǎng)站建設(shè),東營(yíng)區(qū)品牌網(wǎng)站建設(shè),網(wǎng)站定制,東營(yíng)區(qū)網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷(xiāo),網(wǎng)絡(luò)優(yōu)化,東營(yíng)區(qū)網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專(zhuān)業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

這篇文章將為大家詳細(xì)講解有關(guān)mac在matplotlib中如何顯示中文,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

Matplotlib 是一個(gè) Python 的 2D繪圖庫(kù),它以各種硬拷貝格式和跨平臺(tái)的交互式環(huán)境生成出版質(zhì)量級(jí)別的圖形   。
通過(guò) Matplotlib,開(kāi)發(fā)者可以僅需要幾行代碼,便可以生成繪圖,直方圖,功率譜,條形圖,錯(cuò)誤圖,散點(diǎn)圖等。

下面開(kāi)始今天的正文。

首先保證電腦里是否安裝了中文字體,然后找到他們!!

具體步驟如下:

先打開(kāi)終端,command+空格 搜索 ter,然后會(huì)蹦出終端,點(diǎn)開(kāi)

輸入 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

mac在matplotlib中如何顯示中文

#保證中文正常使用
from sklearn.datasets import make_blobs, load_iris
import matplotlib.pyplot as plt


# 支持中文
plt.rcParams['font.sans-serif'] = ['Arial Black'] # 用來(lái)正常顯示中文標(biāo)簽
plt.rcParams['axes.unicode_minus'] = False # 用來(lái)正常顯示負(fù)號(hào)

n_samples = 1000
random_state = 37 #隨機(jī)分割測(cè)試集和訓(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"長(zhǎng)度")
plt.ylabel(u"寬度")
plt.show()

mac在matplotlib中如何顯示中文

知識(shí)點(diǎn)補(bǔ)充:

給大家補(bǔ)充一個(gè)matplotlib中文亂碼問(wèn)題

在ubuntu16.04中使用python的matplotlib模塊進(jìn)行科學(xué)制圖時(shí),在輸出圖例或者標(biāo)題的時(shí)候出現(xiàn)中文亂碼問(wèn)題:

解決:

下載字體:msyh.ttf (微軟雅黑),放在系統(tǒng)字體文件夾下: /usr/share/fonts
同時(shí)我也復(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)

關(guān)于“mac在matplotlib中如何顯示中文”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

名稱欄目:mac在matplotlib中如何顯示中文-創(chuàng)新互聯(lián)
鏈接URL:http://muchs.cn/article34/dcpdse.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、靜態(tài)網(wǎng)站服務(wù)器托管、企業(yè)網(wǎng)站制作、網(wǎng)站設(shè)計(jì)公司、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司