ubuntu系統(tǒng)下matplotlib中文亂碼問題的解決方法
在ubuntu 下利用matplotlib 繪圖的時候,圖像上中文無法顯示。以下是我的解決辦法:
1. 下載中文字體simhei.ttf, 網(wǎng)址為http://fontzone.net/download/simhei
2. 搜索 matplotlib 字體的安裝位置
$locate -b '\mpl-data'
會得到 這個路徑/usr/share/matplotlib/mpl-data下面有fonts/ttf這個目錄,進入這個目錄,把剛才下載的simhei.ttf 字體復制到這個目錄下,注意權(quán)限和歸屬是否與其它字體一致,我的是歸于root用戶的,所以用root 用戶復制過來。
3. 刪除當前用戶matplotlib 的緩沖文件(如果沒有直接進入第四步)
$cd ~/.cache/matplotlib $rm -rf *.*
4.代碼中調(diào)整字體
#!/usr/bin/env python #coding:utf-8 """a demo of matplotlib""" import matplotlib as mpl from matplotlib import pyplot as plt mpl.rcParams[u'font.sans-serif'] = ['simhei'] mpl.rcParams['axes.unicode_minus'] = False years = [1950, 1960, 1970, 1980, 1990, 2000, 2010] gdp = [300.2, 543.3, 1075.9, 2862.5, 5979.6, 10289.7, 14958.3] #創(chuàng)建一副線圖,x軸是年份,y軸是gdp plt.plot(years, gdp, color='green', marker='o', linestyle='solid') #添加一個標題 plt.title(u'名義GDP') #給y軸加標記 plt.ylabel(u'十億美元') plt.show()
- 其中#coding:utf-8 說明文件編碼格式
- mpl.rcParams[u'font.sans-serif'] = ['simhei'] 用simhei 字體顯示中文
- mpl.rcParams['axes.unicode_minus'] = False 這個用來正常顯示負號
- plt.title(u'名義GDP')這里的u 最好不要少
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持本站。
版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。