亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

python設(shè)置 matplotlib 正確顯示中文的四種方式

瀏覽:3日期:2022-06-20 09:46:23
一、前言

啪地一下點(diǎn)進(jìn)來,很快呀~~

python設(shè)置 matplotlib 正確顯示中文的四種方式

matplotlib是 Python 優(yōu)秀的數(shù)據(jù)可視化第三方庫,matplotlib是基于 numpy 的一套 Python 工具包。這個(gè)包提供了豐富的數(shù)據(jù)繪圖工具,主要用于繪制一些統(tǒng)計(jì)圖形。

python設(shè)置 matplotlib 正確顯示中文的四種方式

Matplotlib庫由各種可視化類構(gòu)成,內(nèi)部結(jié)構(gòu)復(fù)雜,受 Matlab 啟發(fā) matplotlib.pyplot 是繪制各類可視化圖形的命令子庫,相當(dāng)于快捷方式。

import matplotlib.pyplot as plt

可 matplotlib 并不支持中文顯示。有中文顯示會(huì)出現(xiàn)如下問題:

# -*- coding: UTF-8 -*-'''@Author :葉庭云@公眾號(hào) :修煉Python@CSDN :https://yetingyun.blog.csdn.net/三折線 黑白灰風(fēng)格 標(biāo)簽label 標(biāo)記點(diǎn)形狀'''import matplotlib.pyplot as plt# 生成x軸數(shù)據(jù) 列表推導(dǎo)式x_data = [i for i in range(0, 55, 5)]# 構(gòu)造y軸數(shù)據(jù)y_data1 = [0.5, 0.62, 0.72, 0.78, 0.85, 0.7, 0.64, 0.44, 0.29, 0.15, 0.09]y_data2 = [0.5, 0.67, 0.71, 0.76, 0.79, 0.66, 0.58, 0.44, 0.38, 0.26, 0.18]y_data3 = [0.5, 0.59, 0.72, 0.74, 0.76, 0.68, 0.58, 0.48, 0.4, 0.36, 0.3]# 設(shè)置圖形顯示風(fēng)格plt.style.use(’ggplot’)# 設(shè)置figure大小 像素plt.figure(figsize=(8, 5), dpi=100)# 繪制三條折線 點(diǎn)的形狀 顏色 標(biāo)簽:用于圖例顯示plt.plot(x_data, y_data1, marker=’^’, color='k', label='設(shè)備1')plt.plot(x_data, y_data2, marker='o', color='k', label='設(shè)備2')plt.plot(x_data, y_data3, marker='s', color='k', label='設(shè)備3')# x y 軸標(biāo)簽 字體大小plt.xlabel('時(shí)間周期/min', fontsize=13)plt.ylabel('直接信任度值', fontsize=13)# 顯示圖例plt.legend()# 保存圖片 展示showplt.savefig('折線圖01.png', dpi=200)plt.show()

可 matplotlib 并不支持中文顯示。有中文顯示會(huì)出現(xiàn)如下問題:

# -*- coding: UTF-8 -*-'''@Author :葉庭云@公眾號(hào) :修煉Python@CSDN :https://yetingyun.blog.csdn.net/三折線 黑白灰風(fēng)格 標(biāo)簽label 標(biāo)記點(diǎn)形狀'''import matplotlib.pyplot as plt# 生成x軸數(shù)據(jù) 列表推導(dǎo)式x_data = [i for i in range(0, 55, 5)]# 構(gòu)造y軸數(shù)據(jù)y_data1 = [0.5, 0.62, 0.72, 0.78, 0.85, 0.7, 0.64, 0.44, 0.29, 0.15, 0.09]y_data2 = [0.5, 0.67, 0.71, 0.76, 0.79, 0.66, 0.58, 0.44, 0.38, 0.26, 0.18]y_data3 = [0.5, 0.59, 0.72, 0.74, 0.76, 0.68, 0.58, 0.48, 0.4, 0.36, 0.3]# 設(shè)置圖形顯示風(fēng)格plt.style.use(’ggplot’)# 設(shè)置figure大小 像素plt.figure(figsize=(8, 5), dpi=100)# 繪制三條折線 點(diǎn)的形狀 顏色 標(biāo)簽:用于圖例顯示plt.plot(x_data, y_data1, marker=’^’, color='k', label='設(shè)備1')plt.plot(x_data, y_data2, marker='o', color='k', label='設(shè)備2')plt.plot(x_data, y_data3, marker='s', color='k', label='設(shè)備3')# x y 軸標(biāo)簽 字體大小plt.xlabel('時(shí)間周期/min', fontsize=13)plt.ylabel('直接信任度值', fontsize=13)# 顯示圖例plt.legend()# 保存圖片 展示showplt.savefig('折線圖01.png', dpi=200)plt.show()

python設(shè)置 matplotlib 正確顯示中文的四種方式

需要我們手動(dòng)一下下設(shè)置~~,才能解決中文顯示的問題。

二、解決方法1. 方式一

from matplotlib.font_manager import FontProperties # 導(dǎo)入FontPropertiesfont = FontProperties(fname='SimHei.ttf', size=14) # 設(shè)置字體# 哪里需要顯示中文就在哪里設(shè)置

# -*- coding: UTF-8 -*-'''@Author :葉庭云@公眾號(hào) :修煉Python@CSDN :https://yetingyun.blog.csdn.net/三折線 黑白灰風(fēng)格 標(biāo)簽label 標(biāo)記點(diǎn)形狀'''import matplotlib.pyplot as pltfrom matplotlib.font_manager import FontProperties # 步驟一# 生成x軸數(shù)據(jù) 列表推導(dǎo)式x_data = [i for i in range(0, 55, 5)]# 構(gòu)造y軸數(shù)據(jù)y_data1 = [0.5, 0.62, 0.72, 0.78, 0.85, 0.7, 0.64, 0.44, 0.29, 0.15, 0.09]y_data2 = [0.5, 0.67, 0.71, 0.76, 0.79, 0.66, 0.58, 0.44, 0.38, 0.26, 0.18]y_data3 = [0.5, 0.59, 0.72, 0.74, 0.76, 0.68, 0.58, 0.48, 0.4, 0.36, 0.3]# 設(shè)置圖形顯示風(fēng)格plt.style.use(’ggplot’)font = FontProperties(fname='SimHei.ttf', size=14) # 步驟二# 設(shè)置figure大小 像素plt.figure(figsize=(8, 5), dpi=100)# 繪制三條折線 點(diǎn)的形狀 顏色 標(biāo)簽:用于圖例顯示plt.plot(x_data, y_data1, marker=’^’, color='k', label='設(shè)備1')plt.plot(x_data, y_data2, marker='o', color='k', label='設(shè)備2')plt.plot(x_data, y_data3, marker='s', color='k', label='設(shè)備3')# x y 軸標(biāo)簽 字體大小plt.xlabel('時(shí)間周期/min', fontsize=13, fontproperties=font)plt.ylabel('直接信任度值', fontsize=13, fontproperties=font)# 顯示圖例plt.legend(prop=font)# 保存圖片 展示showplt.savefig('折線圖01.png', dpi=200)plt.show()

結(jié)果如下:

python設(shè)置 matplotlib 正確顯示中文的四種方式

2. 方式二

通過 fontdict 字典參數(shù)來設(shè)置

fontdict={'family': 'KaiTi', 'size': 15, 'color': 'r'}

# -*- coding: UTF-8 -*-'''@Author :葉庭云@公眾號(hào) :修煉Python@CSDN :https://yetingyun.blog.csdn.net/三折線 黑白灰風(fēng)格 標(biāo)簽label 標(biāo)記點(diǎn)形狀'''import matplotlib.pyplot as plt# 生成x軸數(shù)據(jù) 列表推導(dǎo)式x_data = [i for i in range(0, 55, 5)]# 構(gòu)造y軸數(shù)據(jù)y_data1 = [0.5, 0.62, 0.72, 0.78, 0.85, 0.7, 0.64, 0.44, 0.29, 0.15, 0.09]y_data2 = [0.5, 0.67, 0.71, 0.76, 0.79, 0.66, 0.58, 0.44, 0.38, 0.26, 0.18]y_data3 = [0.5, 0.59, 0.72, 0.74, 0.76, 0.68, 0.58, 0.48, 0.4, 0.36, 0.3]# 設(shè)置圖形顯示風(fēng)格plt.style.use(’ggplot’)# 設(shè)置figure大小 像素plt.figure(figsize=(8, 5), dpi=100)# 繪制三條折線 點(diǎn)的形狀 顏色 標(biāo)簽:用于圖例顯示plt.plot(x_data, y_data1, marker=’^’, color='k', label='設(shè)備1')plt.plot(x_data, y_data2, marker='o', color='k', label='設(shè)備2')plt.plot(x_data, y_data3, marker='s', color='k', label='設(shè)備3')# x y 軸標(biāo)簽 字體大小plt.xlabel('時(shí)間周期/min', fontsize=13, fontdict={'family': 'KaiTi', 'size': 15, 'color': 'r'})plt.ylabel('直接信任度值', fontsize=13, fontdict={'family': 'KaiTi', 'size': 15, 'color': 'k'})# 顯示圖例plt.legend(prop={’family’: ’SimHei’, ’size’: 16})# 保存圖片 展示showplt.savefig('折線圖01.png', dpi=200)plt.show()3. 方式三

改變?nèi)值淖煮w

# matplotlib其實(shí)是不支持顯示中文的 顯示中文需要一行代碼設(shè)置字體mpl.rcParams[’font.family’] = ’SimHei’plt.rcParams[’axes.unicode_minus’] = False # 步驟二(解決坐標(biāo)軸負(fù)數(shù)的負(fù)號(hào)顯示問題)

# -*- coding: UTF-8 -*-'''@Author :葉庭云@公眾號(hào) :修煉Python@CSDN :https://yetingyun.blog.csdn.net/三折線 黑白灰風(fēng)格 標(biāo)簽label 標(biāo)記點(diǎn)形狀'''import matplotlib.pyplot as pltimport matplotlib as mpl# 生成x軸數(shù)據(jù) 列表推導(dǎo)式x_data = [i for i in range(0, 55, 5)]# 構(gòu)造y軸數(shù)據(jù)y_data1 = [0.5, 0.62, 0.72, 0.78, 0.85, 0.7, 0.64, 0.44, 0.29, 0.15, 0.09]y_data2 = [0.5, 0.67, 0.71, 0.76, 0.79, 0.66, 0.58, 0.44, 0.38, 0.26, 0.18]y_data3 = [0.5, 0.59, 0.72, 0.74, 0.76, 0.68, 0.58, 0.48, 0.4, 0.36, 0.3]# matplotlib其實(shí)是不支持顯示中文的 顯示中文需要一行代碼設(shè)置字體mpl.rcParams[’font.family’] = ’SimHei’plt.rcParams[’axes.unicode_minus’] = False # 步驟二(解決坐標(biāo)軸負(fù)數(shù)的負(fù)號(hào)顯示問題)# 設(shè)置圖形顯示風(fēng)格plt.style.use(’ggplot’)# 設(shè)置figure大小 像素plt.figure(figsize=(8, 5), dpi=100)# 繪制三條折線 點(diǎn)的形狀 顏色 標(biāo)簽:用于圖例顯示plt.plot(x_data, y_data1, marker=’^’, color='k', label='設(shè)備1')plt.plot(x_data, y_data2, marker='o', color='k', label='設(shè)備2')plt.plot(x_data, y_data3, marker='s', color='k', label='設(shè)備3')# x y 軸標(biāo)簽 字體大小plt.xlabel('時(shí)間周期/min', fontsize=13)plt.ylabel('直接信任度值', fontsize=13)# 顯示圖例plt.legend()# 保存圖片 展示showplt.savefig('折線圖01.png', dpi=200)plt.show()

結(jié)果如下:

python設(shè)置 matplotlib 正確顯示中文的四種方式

4. 方式四

同樣也是全局改變字體的方法

font = {’family’ : ’SimHei’,’weight’ : ’bold’,’size’ : ’16’}plt.rc(’font’, **font) # 步驟一(設(shè)置字體的更多屬性)plt.rc(’axes’, unicode_minus=False) # 步驟二(解決坐標(biāo)軸負(fù)數(shù)的負(fù)號(hào)顯示問題)

# -*- coding: UTF-8 -*-'''@Author :葉庭云@公眾號(hào) :修煉Python@CSDN :https://yetingyun.blog.csdn.net/三折線 黑白灰風(fēng)格 標(biāo)簽label 標(biāo)記點(diǎn)形狀'''import matplotlib.pyplot as plt# 生成x軸數(shù)據(jù) 列表推導(dǎo)式x_data = [i for i in range(0, 55, 5)]# 構(gòu)造y軸數(shù)據(jù)y_data1 = [0.5, 0.62, 0.72, 0.78, 0.85, 0.7, 0.64, 0.44, 0.29, 0.15, 0.09]y_data2 = [0.5, 0.67, 0.71, 0.76, 0.79, 0.66, 0.58, 0.44, 0.38, 0.26, 0.18]y_data3 = [0.5, 0.59, 0.72, 0.74, 0.76, 0.68, 0.58, 0.48, 0.4, 0.36, 0.3]font = {’family’ : ’SimHei’,’weight’ : ’bold’,’size’ : ’16’}plt.rc(’font’, **font) # 步驟一(設(shè)置字體的更多屬性)plt.rc(’axes’, unicode_minus=False) # 步驟二(解決坐標(biāo)軸負(fù)數(shù)的負(fù)號(hào)顯示問題)# 設(shè)置圖形顯示風(fēng)格plt.style.use(’ggplot’)# 設(shè)置figure大小 像素plt.figure(figsize=(8, 5), dpi=100)# 繪制三條折線 點(diǎn)的形狀 顏色 標(biāo)簽:用于圖例顯示plt.plot(x_data, y_data1, marker=’^’, color='k', label='設(shè)備1')plt.plot(x_data, y_data2, marker='o', color='k', label='設(shè)備2')plt.plot(x_data, y_data3, marker='s', color='k', label='設(shè)備3')# x y 軸標(biāo)簽 字體大小plt.xlabel('時(shí)間周期/min', fontsize=13)plt.ylabel('直接信任度值', fontsize=13)# 顯示圖例plt.legend()# 保存圖片 展示showplt.savefig('折線圖01.png', dpi=200)plt.show()

結(jié)果如下:

python設(shè)置 matplotlib 正確顯示中文的四種方式

三、總結(jié) 方式一、方式二是哪里需要中文顯示才設(shè)置,且不會(huì)污染全局字體設(shè)置,更靈活。 方式三、方式四不改變?nèi)值淖煮w設(shè)置,一次設(shè)置,多次使用,更方便。

附常用字體如下:

宋體:SimSun 黑體:SimHei 微軟雅黑:Microsoft YaHei 微軟正黑體:Microsoft JhengHei 新宋體:NSimSun 新細(xì)明體:PMingLiU 細(xì)明體:MingLiU 標(biāo)楷體:DFKai-SB 仿宋:FangSong 楷體:KaiTi 隸書:LiSu 幼圓:YouYuan 華文細(xì)黑:STXihei 華文楷體:STKaiti 華文宋體:STSong 華文中宋:STZhongsong 華文仿宋:STFangsong 方正舒體:FZShuTi 方正姚體:FZYaoti 華文彩云:STCaiyun 華文琥珀:STHupo 華文隸書:STLiti 華文行楷:STXingkai 華文新魏:STXinwei

以上就是python設(shè)置 matplotlib 正確顯示中文的四種方式的詳細(xì)內(nèi)容,更多關(guān)于python matplotlib 正確顯示中文的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 国产精品福利在线观看秒播 | 色综久久天天综合绕视看 | 久久精品国产2020观看福利色 | 精品日韩欧美国产一区二区 | 精品国产欧美一区二区三区成人 | 欧美黄色一级片免费看 | 国产精品视频免费的 | 日韩精品在线一区 | 欧美日韩一区二区亚洲 | 香蕉网站狼人久久五月亭亭 | 一级毛片特黄久久免费看 | 国产黄大片在线视频 | 亚洲夂夂婷婷色拍ww47 | 国产精品一区二区久久精品 | 国产精品亚洲第一区焦香 | 国产在线精品一区二区三区 | 国产精品福利久久2020 | 亚洲欧美另类日韩 | 国产午夜精品久久久久九九 | 日韩一区二区免费看 | 亚洲高清美女一区二区三区 | 亚洲欧美日韩在线一区二区三区 | 青青青视频精品中文字幕 | 看一级黄色毛片 | 亚洲国产成人久久一区久久 | 久久久这里有精品 | 国产高清一区二区 | 古代的一a一片一级一片 | 边吃奶边弄进去男女视频 | 亚洲欧美日韩国产综合久 | 欧美亚洲综合另类在线观看 | 善良的翁熄日本在线观看 | 请以你的名字呼唤我免费观看 | 日本xxx护士与黑人 日本xxx免费视频 | 国产精品综合一区二区三区 | 手机看片日韩国产福利视频 | 欧美日韩亚洲一区二区三区在线观看 | 欧美日韩性视频一区二区三区 | 国产1024在线永久免费观看 | 久久久久久国产精品三级 | 在线观看国产一区二区三区 |