python設置 matplotlib 正確顯示中文的四種方式
啪地一下點進來,很快呀~~
matplotlib是 Python 優秀的數據可視化第三方庫,matplotlib是基于 numpy 的一套 Python 工具包。這個包提供了豐富的數據繪圖工具,主要用于繪制一些統計圖形。
Matplotlib庫由各種可視化類構成,內部結構復雜,受 Matlab 啟發 matplotlib.pyplot 是繪制各類可視化圖形的命令子庫,相當于快捷方式。
import matplotlib.pyplot as plt
可 matplotlib 并不支持中文顯示。有中文顯示會出現如下問題:
# -*- coding: UTF-8 -*-'''@Author :葉庭云@公眾號 :修煉Python@CSDN :https://yetingyun.blog.csdn.net/三折線 黑白灰風格 標簽label 標記點形狀'''import matplotlib.pyplot as plt# 生成x軸數據 列表推導式x_data = [i for i in range(0, 55, 5)]# 構造y軸數據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()# 保存圖片 展示showplt.savefig('折線圖01.png', dpi=200)plt.show()
可 matplotlib 并不支持中文顯示。有中文顯示會出現如下問題:
# -*- coding: UTF-8 -*-'''@Author :葉庭云@公眾號 :修煉Python@CSDN :https://yetingyun.blog.csdn.net/三折線 黑白灰風格 標簽label 標記點形狀'''import matplotlib.pyplot as plt# 生成x軸數據 列表推導式x_data = [i for i in range(0, 55, 5)]# 構造y軸數據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()# 保存圖片 展示showplt.savefig('折線圖01.png', dpi=200)plt.show()
需要我們手動一下下設置~~,才能解決中文顯示的問題。
二、解決方法1. 方式一from matplotlib.font_manager import FontProperties # 導入FontPropertiesfont = FontProperties(fname='SimHei.ttf', size=14) # 設置字體# 哪里需要顯示中文就在哪里設置
# -*- coding: UTF-8 -*-'''@Author :葉庭云@公眾號 :修煉Python@CSDN :https://yetingyun.blog.csdn.net/三折線 黑白灰風格 標簽label 標記點形狀'''import matplotlib.pyplot as pltfrom matplotlib.font_manager import FontProperties # 步驟一# 生成x軸數據 列表推導式x_data = [i for i in range(0, 55, 5)]# 構造y軸數據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)# 保存圖片 展示showplt.savefig('折線圖01.png', dpi=200)plt.show()
結果如下:
通過 fontdict 字典參數來設置
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軸數據 列表推導式x_data = [i for i in range(0, 55, 5)]# 構造y軸數據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})# 保存圖片 展示showplt.savefig('折線圖01.png', dpi=200)plt.show()3. 方式三
改變全局的字體
# matplotlib其實是不支持顯示中文的 顯示中文需要一行代碼設置字體mpl.rcParams[’font.family’] = ’SimHei’plt.rcParams[’axes.unicode_minus’] = False # 步驟二(解決坐標軸負數的負號顯示問題)
# -*- coding: UTF-8 -*-'''@Author :葉庭云@公眾號 :修煉Python@CSDN :https://yetingyun.blog.csdn.net/三折線 黑白灰風格 標簽label 標記點形狀'''import matplotlib.pyplot as pltimport matplotlib as mpl# 生成x軸數據 列表推導式x_data = [i for i in range(0, 55, 5)]# 構造y軸數據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 # 步驟二(解決坐標軸負數的負號顯示問題)# 設置圖形顯示風格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()# 保存圖片 展示showplt.savefig('折線圖01.png', dpi=200)plt.show()
結果如下:
同樣也是全局改變字體的方法
font = {’family’ : ’SimHei’,’weight’ : ’bold’,’size’ : ’16’}plt.rc(’font’, **font) # 步驟一(設置字體的更多屬性)plt.rc(’axes’, unicode_minus=False) # 步驟二(解決坐標軸負數的負號顯示問題)
# -*- coding: UTF-8 -*-'''@Author :葉庭云@公眾號 :修煉Python@CSDN :https://yetingyun.blog.csdn.net/三折線 黑白灰風格 標簽label 標記點形狀'''import matplotlib.pyplot as plt# 生成x軸數據 列表推導式x_data = [i for i in range(0, 55, 5)]# 構造y軸數據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) # 步驟二(解決坐標軸負數的負號顯示問題)# 設置圖形顯示風格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()# 保存圖片 展示showplt.savefig('折線圖01.png', dpi=200)plt.show()
結果如下:
附常用字體如下:
宋體: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 正確顯示中文的四種方式的詳細內容,更多關于python matplotlib 正確顯示中文的資料請關注好吧啦網其它相關文章!
相關文章: