最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

matplotlib 生成的圖像中無法顯示中文字符的解決方法

 更新時間:2020年06月10日 09:51:36   作者:Rickyzcm  
這篇文章主要介紹了matplotlib 生成的圖像中無法顯示中文字符的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

前幾天使用matplotlib 繪圖的時候發(fā)現(xiàn)無法使用中文字符,所以找了個筆記,順便分享給大家

開發(fā)環(huán)境

  • windows 8.1 64bit
  • python 3.6.0
  • matplotlib 3.2.1

問題背景

使用 matplotlib 繪制函數(shù)圖像的時候,發(fā)現(xiàn)設(shè)置圖像名稱或圖例需要漢字顯示的時候只能得到空格

生成圖像中的中文錯誤效果

原因分析

python中的matplotlib僅支持Unicode編碼,默認是不顯示中文的.

解決方案

解決方案1

python文件中添上一段語句

plt.rcParams['font.sans-serif']=['Simhei']

之后再次運行得出圖像 

解決方案2

制定加載本地的字體文件

在python文件中導入matplotlib的字體控制方法

from matplotlib.font_manager import FontProperties

另設(shè)font變量存儲設(shè)置好的屬性

font = FontProperties(fname=r'C:\Windows\Fonts\simhei.ttf',size=14)

設(shè)置 title 內(nèi)容時將 font的存儲對象賦給 fontproperties 屬性

plt.title(u'y=x 與 y=x^0.5 的函數(shù)圖像比較',fontproperties = font)

修改源碼后生成的圖像

附件

修改前的 python 文件

 import numpy as np
 import matplotlib.pyplot as plt

 x = np.linspace(0,100) # 設(shè)置自變量的取值[0,100]

 y1 = x
 y2 = x**0.5

 plt.figure()
 plt.plot(x,y1,label='y=x')
 plt.plot(x,y2,label='y=x^0.5',color='red')

 plt.title('y=x 與 y=x^0.5 的函數(shù)圖像比較')

 plt.legend()
 plt.show()

經(jīng)解決方案1修改后的源碼

 import numpy as np
 import matplotlib.pyplot as plt

 x = np.linspace(0,100) # 設(shè)置自變量的取值[0,100]

 y1 = x
 y2 = x**0.5

 plt.figure()
 plt.plot(x,y1,label='y=x')
 plt.plot(x,y2,label='y=x^0.5',color='red')

 plt.title('y=x 與 y=x^0.5 的函數(shù)圖像比較')
 plt.rcParams['font.sans-serif']=['Simhei']

 plt.legend()
 plt.show()

經(jīng)解決方案2修改后的源碼

 import numpy as np
 from matplotlib.font_manager import FontProperties
 import matplotlib.pyplot as plt

 x = np.linspace(0,100) # 設(shè)置自變量的取值[0,100]
 y1 = x
 y2 = x**0.5

 plt.figure()
 plt.plot(x,y1,label='y=x')
 plt.plot(x,y2,label='y=x^0.5',color='red')
 font = FontProperties(fname=r'C:\Windows\Fonts\simhei.ttf',size=14)

 plt.title(u'y=x 與 y=x^0.5 的函數(shù)圖像比較',fontproperties = font)

 plt.legend()
 plt.show()

到此這篇關(guān)于matplotlib 生成的圖像中無法顯示中文字符的解決方法的文章就介紹到這了,更多相關(guān)matplotlib圖像無法顯示中文內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

黎平县| 浑源县| 灵武市| 台江县| 苏州市| 磐石市| 汤阴县| 龙陵县| 禄劝| 抚远县| 诸城市| 桐城市| 遂川县| 武冈市| 井研县| 瓮安县| 芦溪县| 游戏| 井陉县| 邢台市| 吉木乃县| 乾安县| 米脂县| 南郑县| 贡嘎县| 合江县| 呼玛县| 抚宁县| 宜君县| 昌宁县| 廊坊市| 彭阳县| 大丰市| 司法| 长葛市| 五寨县| 石狮市| 库伦旗| 武夷山市| 措勤县| 米泉市|