最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python設置 matplotlib 正確顯示中文的四種方式

 更新時間:2021年05月10日 15:57:55   作者:葉庭云  
這篇文章主要介紹了python設置 matplotlib 正確顯示中文的四種方式,幫助大家更好的理解和學習使用python,感興趣的朋友可以了解下

一、前言

啪地一下點進來,很快呀~~

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

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

import matplotlib.pyplot as plt

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

# -*- coding: UTF-8 -*-
"""
@Author  :葉庭云
@公眾號  :修煉Python
@CSDN    :https://yetingyun.blog.csdn.net/
三折線  黑白灰風格  標簽label 標記點形狀
"""
import matplotlib.pyplot as plt

# 生成x軸數(shù)據(jù)  列表推導式
x_data = [i for i in range(0, 55, 5)]

# 構造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]

# 設置圖形顯示風格
plt.style.use('ggplot')

# 設置figure大小  像素
plt.figure(figsize=(8, 5), dpi=100)

# 繪制三條折線  點的形狀 顏色  標簽:用于圖例顯示
plt.plot(x_data, y_data1, marker='^', color="k", label="設備1")
plt.plot(x_data, y_data2, marker="o", color="k", label="設備2")
plt.plot(x_data, y_data3, marker="s", color="k", label="設備3")

# x y 軸標簽   字體大小
plt.xlabel("時間周期/min", fontsize=13)
plt.ylabel("直接信任度值", fontsize=13)
# 顯示圖例
plt.legend()

# 保存圖片  展示show
plt.savefig("折線圖01.png", dpi=200)
plt.show()


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

# -*- coding: UTF-8 -*-
"""
@Author  :葉庭云
@公眾號  :修煉Python
@CSDN    :https://yetingyun.blog.csdn.net/
三折線  黑白灰風格  標簽label 標記點形狀
"""
import matplotlib.pyplot as plt

# 生成x軸數(shù)據(jù)  列表推導式
x_data = [i for i in range(0, 55, 5)]

# 構造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]

# 設置圖形顯示風格
plt.style.use('ggplot')

# 設置figure大小  像素
plt.figure(figsize=(8, 5), dpi=100)

# 繪制三條折線  點的形狀 顏色  標簽:用于圖例顯示
plt.plot(x_data, y_data1, marker='^', color="k", label="設備1")
plt.plot(x_data, y_data2, marker="o", color="k", label="設備2")
plt.plot(x_data, y_data3, marker="s", color="k", label="設備3")

# x y 軸標簽   字體大小
plt.xlabel("時間周期/min", fontsize=13)
plt.ylabel("直接信任度值", fontsize=13)
# 顯示圖例
plt.legend()

# 保存圖片  展示show
plt.savefig("折線圖01.png", dpi=200)
plt.show()


需要我們手動一下下設置~~,才能解決中文顯示的問題。

二、解決方法

1. 方式一

from matplotlib.font_manager import FontProperties  # 導入FontProperties

font = FontProperties(fname="SimHei.ttf", size=14)  # 設置字體

# 哪里需要顯示中文就在哪里設置
# -*- coding: UTF-8 -*-
"""
@Author  :葉庭云
@公眾號  :修煉Python
@CSDN    :https://yetingyun.blog.csdn.net/
三折線  黑白灰風格  標簽label 標記點形狀
"""
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties  # 步驟一
# 生成x軸數(shù)據(jù)  列表推導式
x_data = [i for i in range(0, 55, 5)]

# 構造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]

# 設置圖形顯示風格
plt.style.use('ggplot')
font = FontProperties(fname="SimHei.ttf", size=14)  # 步驟二
# 設置figure大小  像素
plt.figure(figsize=(8, 5), dpi=100)

# 繪制三條折線  點的形狀 顏色  標簽:用于圖例顯示
plt.plot(x_data, y_data1, marker='^', color="k", label="設備1")
plt.plot(x_data, y_data2, marker="o", color="k", label="設備2")
plt.plot(x_data, y_data3, marker="s", color="k", label="設備3")

# x y 軸標簽   字體大小
plt.xlabel("時間周期/min", fontsize=13, fontproperties=font)
plt.ylabel("直接信任度值", fontsize=13, fontproperties=font)
# 顯示圖例
plt.legend(prop=font)

# 保存圖片  展示show
plt.savefig("折線圖01.png", dpi=200)
plt.show()

結(jié)果如下:

2. 方式二

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

fontdict={"family": "KaiTi", "size": 15, "color": "r"}
# -*- coding: UTF-8 -*-
"""
@Author  :葉庭云
@公眾號  :修煉Python
@CSDN    :https://yetingyun.blog.csdn.net/
三折線  黑白灰風格  標簽label 標記點形狀
"""
import matplotlib.pyplot as plt

# 生成x軸數(shù)據(jù)  列表推導式
x_data = [i for i in range(0, 55, 5)]

# 構造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]

# 設置圖形顯示風格
plt.style.use('ggplot')

# 設置figure大小  像素
plt.figure(figsize=(8, 5), dpi=100)

# 繪制三條折線  點的形狀 顏色  標簽:用于圖例顯示
plt.plot(x_data, y_data1, marker='^', color="k", label="設備1")
plt.plot(x_data, y_data2, marker="o", color="k", label="設備2")
plt.plot(x_data, y_data3, marker="s", color="k", label="設備3")

# x y 軸標簽   字體大小
plt.xlabel("時間周期/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})

# 保存圖片  展示show
plt.savefig("折線圖01.png", dpi=200)
plt.show()

3. 方式三

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

# matplotlib其實是不支持顯示中文的 顯示中文需要一行代碼設置字體
mpl.rcParams['font.family'] = 'SimHei'
plt.rcParams['axes.unicode_minus'] = False   # 步驟二(解決坐標軸負數(shù)的負號顯示問題)
# -*- coding: UTF-8 -*-
"""
@Author  :葉庭云
@公眾號  :修煉Python
@CSDN    :https://yetingyun.blog.csdn.net/
三折線  黑白灰風格  標簽label 標記點形狀
"""
import matplotlib.pyplot as plt
import matplotlib as mpl

# 生成x軸數(shù)據(jù)  列表推導式
x_data = [i for i in range(0, 55, 5)]

# 構造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其實是不支持顯示中文的 顯示中文需要一行代碼設置字體
mpl.rcParams['font.family'] = 'SimHei'
plt.rcParams['axes.unicode_minus'] = False   # 步驟二(解決坐標軸負數(shù)的負號顯示問題)
# 設置圖形顯示風格
plt.style.use('ggplot')

# 設置figure大小  像素
plt.figure(figsize=(8, 5), dpi=100)

# 繪制三條折線  點的形狀 顏色  標簽:用于圖例顯示
plt.plot(x_data, y_data1, marker='^', color="k", label="設備1")
plt.plot(x_data, y_data2, marker="o", color="k", label="設備2")
plt.plot(x_data, y_data3, marker="s", color="k", label="設備3")

# x y 軸標簽   字體大小
plt.xlabel("時間周期/min", fontsize=13)
plt.ylabel("直接信任度值", fontsize=13)

# 顯示圖例
plt.legend()

# 保存圖片  展示show
plt.savefig("折線圖01.png", dpi=200)
plt.show()

結(jié)果如下:

4. 方式四

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

font = {'family' : 'SimHei',
        'weight' : 'bold',
        'size'   : '16'}
plt.rc('font', **font)               # 步驟一(設置字體的更多屬性)
plt.rc('axes', unicode_minus=False)  # 步驟二(解決坐標軸負數(shù)的負號顯示問題)
# -*- coding: UTF-8 -*-
"""
@Author  :葉庭云
@公眾號  :修煉Python
@CSDN    :https://yetingyun.blog.csdn.net/
三折線  黑白灰風格  標簽label 標記點形狀
"""
import matplotlib.pyplot as plt

# 生成x軸數(shù)據(jù)  列表推導式
x_data = [i for i in range(0, 55, 5)]

# 構造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)               # 步驟一(設置字體的更多屬性)
plt.rc('axes', unicode_minus=False)  # 步驟二(解決坐標軸負數(shù)的負號顯示問題)

# 設置圖形顯示風格
plt.style.use('ggplot')

# 設置figure大小  像素
plt.figure(figsize=(8, 5), dpi=100)

# 繪制三條折線  點的形狀 顏色  標簽:用于圖例顯示
plt.plot(x_data, y_data1, marker='^', color="k", label="設備1")
plt.plot(x_data, y_data2, marker="o", color="k", label="設備2")
plt.plot(x_data, y_data3, marker="s", color="k", label="設備3")

# x y 軸標簽   字體大小
plt.xlabel("時間周期/min", fontsize=13)
plt.ylabel("直接信任度值", fontsize=13)

# 顯示圖例
plt.legend()

# 保存圖片  展示show
plt.savefig("折線圖01.png", dpi=200)
plt.show()

結(jié)果如下:

三、總結(jié)

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

附常用字體如下:

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

以上就是python設置 matplotlib 正確顯示中文的四種方式的詳細內(nèi)容,更多關于python matplotlib 正確顯示中文的資料請關注腳本之家其它相關文章!

相關文章

  • Python從文件中讀取數(shù)據(jù)的方法步驟

    Python從文件中讀取數(shù)據(jù)的方法步驟

    這篇文章主要介紹了Python從文件中讀取數(shù)據(jù)的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11
  • Python入門篇之字符串

    Python入門篇之字符串

    可能大多數(shù)人在學習C語言的時候,最先接觸的數(shù)據(jù)類型就是字符串,因為大多教程都是以"Hello world"這個程序作為入門程序,這個程序中要打印的"Hello world"就是字符串。今天我們來了解一下Python中的字符串,看看它的用法。
    2014-10-10
  • python字典的元素訪問實例詳解

    python字典的元素訪問實例詳解

    在本篇文章里小編給大家整理的是一篇關于python字典的元素訪問實例詳解內(nèi)容,有需要的朋友們可以跟著學習參考下。
    2021-07-07
  • Django?RestFramework?全局異常處理詳解

    Django?RestFramework?全局異常處理詳解

    這篇文章主要為大家詳細介紹了Django?RestFramework?全局異常處理,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • Python字符串處理之count()方法的使用

    Python字符串處理之count()方法的使用

    這篇文章主要介紹了Python字符串處理之count()方法的使用,是Python入門的基礎知識,需要的朋友可以參考下
    2015-05-05
  • 詳談python中subprocess shell=False與shell=True的區(qū)別

    詳談python中subprocess shell=False與shell=True的區(qū)別

    這篇文章主要介紹了詳談python中subprocess shell=False與shell=True的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • 卷積神經(jīng)網(wǎng)絡經(jīng)典模型及其改進點學習匯總

    卷積神經(jīng)網(wǎng)絡經(jīng)典模型及其改進點學習匯總

    這篇文章主要為大家介紹了卷積神經(jīng)網(wǎng)絡經(jīng)典模型及其改進點學習匯總,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-05-05
  • keras中的loss、optimizer、metrics用法

    keras中的loss、optimizer、metrics用法

    這篇文章主要介紹了keras中的loss、optimizer、metrics用法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • python使用tomorrow實現(xiàn)多線程的例子

    python使用tomorrow實現(xiàn)多線程的例子

    今天小編就為大家分享一篇python使用tomorrow實現(xiàn)多線程的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • python中子類與父類的關系基礎知識點

    python中子類與父類的關系基礎知識點

    在本篇文章里小編給大家整理的是一篇關于python中子類與父類的關系基礎知識點內(nèi)容,對此有興趣的朋友們可以學習下。
    2021-02-02

最新評論

信宜市| 鹿泉市| 潜山县| 茶陵县| 涿州市| 安远县| 绥宁县| 丰原市| 海门市| 五莲县| 资源县| 托克逊县| 上蔡县| 望城县| 墨竹工卡县| 洪泽县| 铁力市| 抚松县| 武汉市| 永州市| 崇义县| 阿巴嘎旗| 四子王旗| 呼图壁县| 洛浦县| 寻乌县| 平阴县| 肃南| 阿克| 杭州市| 泰顺县| 新蔡县| 桃园市| 东平县| 江阴市| 常熟市| 三都| 同仁县| 泉州市| 夏津县| 岐山县|