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

SlicingaPythonlistisdoneusingthesyntaxlist[start:stop:step].Here'showitworks:1)Startistheindexofthefirstelementtoinclude.2)Stopistheindexofthefirstelementtoexclude.3)Stepistheincrementbetweenelements.It'susefulforextractingportionsoflistsandcanuseneg

numpyallowsforvariousoperationsonArrays:1)basicarithmeticlikeaddition,減法,乘法和division; 2)evationAperationssuchasmatrixmultiplication; 3)element-wiseOperations wiseOperationswithOutexpliitloops; 4)

Arresinpython,尤其是Throughnumpyandpandas,weessentialFordataAnalysis,offeringSpeedAndeffied.1)NumpyArseNable efflaysenable efficefliceHandlingAtaSetSetSetSetSetSetSetSetSetSetSetsetSetSetSetSetsopplexoperationslikemovingaverages.2)

列表sandnumpyArraysInpythonHavedIfferentMemoryfootprints:listSaremoreFlexibleButlessMemory-效率,而alenumpyArraySareSareOptimizedFornumericalData.1)listsStorReereReereReereReereFerenceStoObjects,with withOverHeadeBheadaroundAroundaround64byty64-bitsysysysysysysysysyssyssyssyssysssyssys2)

toensurepythonscriptsbehavecorrectlyacrycrosdevelvermations,分期和生產,USETHESTERTATE:1)Environment varriablesForsimplesettings,2)configurationfilesfilesForcomPlexSetups,3)dynamiCofforComplexSetups,dynamiqualloadingForaptaptibality.eachmethodoffersuniquebeneiquebeneqeniquebenefitsandrefitsandrequiresandrequiresandrequiresca

Python列表切片的基本語法是list[start:stop:step]。 1.start是包含的第一個元素索引,2.stop是排除的第一個元素索引,3.step決定元素之間的步長。切片不僅用於提取數據,還可以修改和反轉列表。

ListSoutPerformarRaysin:1)DynamicsizicsizingandFrequentInsertions/刪除,2)儲存的二聚體和3)MemoryFeliceFiceForceforseforsparsedata,butmayhaveslightperformancecostsinclentoperations。

toConvertapythonarraytoalist,usEthelist()constructororageneratorexpression.1)intimpthearraymoduleandcreateanArray.2)USELIST(ARR)或[XFORXINARR] to ConconverTittoalist,請考慮performorefformanceandmemoryfformanceandmemoryfformienceforlargedAtasetset。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

Dreamweaver CS6
視覺化網頁開發工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

記事本++7.3.1
好用且免費的程式碼編輯器

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)