欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

mac在matplotlib中顯示中文的操作方法

 更新時間:2020年03月06日 15:55:42   作者:啥都不會可咋整  
這篇文章主要介紹了mac如何在matplotlib中顯示中文,本文分步驟給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

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

下面開始今天的正文。

首先保證電腦里是否安裝了中文字體,然后找到他們?。?/p>

具體步驟如下:

先打開終端,command+空格 搜索 ter,然后會蹦出終端,點開

輸入 fc-list :lang=zh

如果顯示command not found

輸入 conda install fontconfig

然后輸入 y

然后就安裝好了?。。。?/p>

再輸入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'] # 用來正常顯示中文標簽
plt.rcParams['axes.unicode_minus'] = False # 用來正常顯示負號

n_samples = 1000
random_state = 37 #隨機分割測試集和訓練集

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模塊進行科學制圖時,在輸出圖例或者標題的時候出現(xiàn)中文亂碼問題:

解決:

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

總結

到此這篇關于mac如何在matplotlib中顯示中文的文章就介紹到這了,更多相關mac matplotlib 中文內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論