這篇文章主要介紹了解決Linux系統中python matplotlib畫圖的中文顯示問題,需要的朋友可以參考下
最近想學習一些python數據分析的內容,就弄了個爬蟲爬取了一些數據,並打算用Anaconda一套的工具(pandas, numpy, scipy, matplotlib, jupyter)等進行一些初步的數據挖掘和分析。
在使用matplotlib畫圖時,橫座標為中文,但是畫出的條形圖橫座標總是顯示“框框”,就去查資料解決。感覺這應該是個比較常見的問題,網路上的中文資料也確實很多,但是沒有任何一個徹底解決了我遇到的問題。零零碎碎花了快3小時的時間,才終於搞定。特此分享,希望能幫上有相同問題的童鞋。
運行環境:
#python2.7
- ##Linux Centos7
- 用conda
##問題:
- matplotlib畫圖,無法顯示中文
#問題原因:
- linux作業系統以及matplotlib的字型庫中,沒有可用的中文字體
- matplotlib套件預設只支援ASCII碼,不支援unicode碼
- 線上資料總結:
#其實也是綜合了網路上的各種解決方案。整體說來有以下步驟:#1. 取得matplotlibrc檔案所在路徑。在jupyter notebook中取得:import matplotlib
matplotlib.matplotlib_fname()
例如,我的這個檔案在:
u'~/miniconda2/lib/python2.7/site-packages/matplotlib/mpl-data/matplotlibrc'
後續步驟會修改此檔案中的font參數。
2. 看看系統中的所有字體,以及可用的中文字體。也是在jupyter nb中:
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, '系统可用的中文字体', '*' * 10 print output zh_fonts = set(f.split(',', 1)[0] for f in output.split('\n')) available = mat_fonts & zh_fonts print '*' * 10, '可用的字体', '*' * 10 for f in available: print f
做完上述操作,會發現「可用的字體」這裡為空。因為沒有中文字體給matplotlib用(所以才會中文都顯示「框框」)
3. 假設作業系統中沒有中文字體。此時下載一個ttf中文字體,並在cenos中安裝。要安裝那種系統能偵測
font-family的,否則無效。我在這個網站下載的:http://font.chinaz.com/130130474870.htm
解壓縮rar檔。在/usr/share/fonts 路徑下建立存放此字體的資料夾yourfontdir,並下載的ttf檔案複製到yourfontdir(可以給檔案改個英文名,方便操作)
#4. 為cenos安裝這個字體。 cd /usr/share/fonts/yourfontsdir
#生成字体索引信息. 会显示字体的font-family
sudo mkfontscale
sudo mkfontdir
#更新字体缓存:
fc-cache
5. 修改matplotlibrc檔案
修改步驟1中取得的matplotlibrc檔案配置。
將font.family 部分註解去掉,並且在font.serif 支援字體加上一個中文字體。這裡就加上剛才下載的中文字體的font-family. 可以透過 fc-list 指令找找(所以前面最好記下來)。我這裡增加的是"WenQuanYi Zen Hei Mono"字體。
下面這句註解要去掉,不然中文減號也顯示方塊:
axes.unicode_minus : False
6. 這步驟最重要!為matplotlib增加中文字體
完成步驟5後,再操作步驟2,會發現「可用的中文字體」已經有了剛才安裝的字體,但是畫圖仍然不能顯示中文。這是因為你這個字體給centos安裝了、也告訴matplotlib要用這個字體了,但是,matplotlib找不到這個字體的ttf檔啊。 。 。 。所以需要給它弄一個。
將下載的ttf字型複製一份到以下路徑:
~/miniconda2/lib/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf
並
刪除相關cache。在以下路徑:
~/.cache/matplotlib
刪除其中與字體相關的cache
以上是分享一篇Linux系統下python matplotlib畫圖的中文顯示實例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

pythonisehybridmodeLofCompilation和interpretation:1)thepythoninterpretercompilesourcecececodeintoplatform- interpententbybytecode.2)thepythonvirtualmachine(pvm)thenexecutecutestestestestestesthisbytecode,ballancingEaseofuseEfuseWithPerformance。

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允許fordingfordforderynamictynamictymictymictymictyandrapiddefupment,儘管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

在您的知識之際,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations則youneedtoloopuntilaconditionismet

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond

pythonisnotpuroly interpred; itosisehybridablectofbytecodecompilationandruntimeinterpretation.1)PythonCompiLessourceceCeceDintobyTecode,whitsthenexecececected bytybytybythepythepythepythonvirtirtualmachine(pvm).2)

concatenateListSinpythonWithTheSamelements,使用:1)operatoTotakeEpduplicates,2)asettoremavelemavphicates,or3)listcompreanspherensionforcontroloverduplicates,每個methodhasdhasdifferentperferentperferentperforentperforentperforentperfornceandordorimplications。

pythonisanterpretedlanguage,offeringosofuseandflexibilitybutfacingperformancelanceLimitationsInCricapplications.1)drightingedlanguageslikeLikeLikeLikeLikeLikeLikeLikeThonexecuteline-by-line,允許ImmediaMediaMediaMediaMediaMediateFeedBackAndBackAndRapidPrototypiD.2)compiledLanguagesLanguagesLagagesLikagesLikec/c thresst

Useforloopswhenthenumberofiterationsisknowninadvance,andwhileloopswheniterationsdependonacondition.1)Forloopsareidealforsequenceslikelistsorranges.2)Whileloopssuitscenarioswheretheloopcontinuesuntilaspecificconditionismet,usefulforuserinputsoralgorit


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能