Matplotlib中文亂碼的3種解決方案
前言
Matplotlib是一個(gè)Python 2D繪圖庫(kù),它可以在各種平臺(tái)上以各種硬拷貝格式和交互式環(huán)境生成出具有出版品質(zhì)的圖形。 Matplotlib可用于Python腳本,Python和IPython shell,Jupyter筆記本,Web應(yīng)用程序服務(wù)器和四個(gè)圖形用戶界面工具包。
然而最近在使用matplotlib默認(rèn)情況會(huì)出現(xiàn)亂碼問(wèn)題,原則上matplotlib是支持中文的,只是在配置信息里沒(méi)有中文字體的相關(guān)信息。
解決方法如下:
解決方案一:修改配置文件
matplotlib 從配置文件 matplotlibrc 中讀取配置,字體相關(guān)內(nèi)容也在其中。查詢當(dāng)前matplotlibrc 所在目錄,可以用 get_configdir()函數(shù):
import matplotlib matplotlib.get_configdir()
通常存放位置:lib\site-packages\matplotlib\mpl-data\matplotlibrc
涉及到字體部分的設(shè)置內(nèi)容為:
#font.family : sans-serif #font.style : normal #font.variant : normal #font.weight : normal #font.stretch : normal ## note that font.size controls default text sizes. To configure ## special text sizes tick labels, axes, labels, title, etc, see the rc ## settings for axes and ticks. Special text sizes can be defined ## relative to font.size, using the following values: xx-small, x-small, ## small, medium, large, x-large, xx-large, larger, or smaller #font.size : 10.0 #font.serif : DejaVu Serif, Bitstream Vera Serif, Computer Modern Roman, New Century Schoolbook, Century Schoolbook L, Utopia, ITC Bookman, Bookman, Nimbus Roman No9 L, Times New Roman, Times, Palatino, Charter, serif #font.sans-serif : DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif #font.cursive : Apple Chancery, Textile, Zapf Chancery, Sand, Script MT, Felipa, cursive #font.fantasy : Comic Sans MS, Chicago, Charcoal, ImpactWestern, Humor Sans, xkcd, fantasy #font.monospace : DejaVu Sans Mono, Bitstream Vera Sans Mono, Computer Modern Typewriter, Andale Mono, Nimbus Mono L, Courier New, Courier, Fixed, Terminal, monospace
matplotlib 默認(rèn)使用的 font.family 是 sans-serif,即無(wú)襯線字體,可以看到在font.sans-serif中設(shè)置的全部為西文字體,這里的設(shè)置和css樣式文件中設(shè)置差不多,只需要添加系統(tǒng)存在的字體名稱即可(需要注意的是,matplotlib:
只支持ttf格式的字體),設(shè)置時(shí)需要將注釋符號(hào)#去除。
解決方案二:重載配置文件
import matplotlib as mpl mpl.rcParams['font.sans-serif'] = ['SimHei'] mpl.rcParams['font.serif'] = ['SimHei'] mpl.rcParams['axes.unicode_minus'] = False # 解決保存圖像是負(fù)號(hào)'-'顯示為方塊的問(wèn)題,或者轉(zhuǎn)換負(fù)號(hào)為字符串
解決方案三:自定義字體
import numpy as np import pylab as pl import matplotlib.font_manager as fm myfont = fm.FontProperties(fname=r'D:\Fonts\simkai.ttf') # 設(shè)置字體 t = np.arange(0.0,2.0 * np.pi,0.01) # 自變量取值范圍 s = np.sin(t) # 計(jì)算正弦函數(shù)值 z = np.cos(t) # 計(jì)算余弦函數(shù)值 pl.plot(t,s,label='正弦') pl.plot(t,z,label='余弦') pl.xlabel('x-變量',fontproperties=myfont,fontsize=24) #設(shè)置標(biāo)簽 pl.ylabel('y-正弦余弦函數(shù)值',fontproperties=myfont,fontsize=24) pl.title('sin-cos函數(shù)圖像',fontproperties=myfont,fontsize=32) #圖像標(biāo)題 pl.legend(prop=myfont) pl.show()
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
python中列表(list)和元組(tuple)的深入講解
這篇文章主要給大家介紹了關(guān)于python中列表(list)和元組(tuple)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03python3?cookbook解壓可迭代對(duì)象賦值給多個(gè)變量的問(wèn)題及解決方案
這篇文章主要介紹了python3?cookbook-解壓可迭代對(duì)象賦值給多個(gè)變量,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-01-01conda?install?nb_conda失敗原因分析及解決
這篇文章主要給大家介紹了關(guān)于conda?install?nb_conda失敗原因分析及解決方法,conda install nb_conda顯示錯(cuò)誤的原因可能有很多,具體原因取決于你的系統(tǒng)環(huán)境和安裝的conda版本,需要的朋友可以參考下2023-11-11詳解Python如何使用Self類型實(shí)現(xiàn)返回類的實(shí)例對(duì)象
在 Python 中,類方法通常會(huì)返回類的實(shí)例對(duì)象,本文將詳細(xì)介紹如何在 Python 中使用 Self 類型來(lái)返回類的實(shí)例對(duì)象,并提供豐富的示例代碼幫助更好地理解,快跟隨小編一起學(xué)習(xí)起來(lái)吧2024-02-02