<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Matplotlib支持中文顯示的兩種方法

          共 3741字,需瀏覽 8分鐘

           ·

          2021-05-25 23:34

          在默認(rèn)情況下,Matplotlib在設(shè)置title和標(biāo)注text時如果使用中文,會出現(xiàn)尷尬的框框。

          使用以下兩種方法可以輕松化解尷尬,讓您在使用Matplotlib繪圖時展露愉悅笑容。

          1,使用FontManager函數(shù)指定中文字體文件

          2,將中文字體文件放入matplotlib安裝目錄下

          在公眾號算法美食屋后臺回復(fù)關(guān)鍵字:源碼,可以獲取本文全部代碼。

          一,使用FontManager函數(shù)指定中文字體文件

          這種方法步驟較少,但是需要在每一個使用中文的地方指定font參數(shù)。

          step1:下載中文字體文件

          在公眾號算法美食屋后臺回復(fù)關(guān)鍵字:中文字體,可以獲取SimHei中文字體。

          step2:在代碼中用FontManager函數(shù)指定中文字體文件路徑。

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

          # 加載中文字體
          font = FontProperties(fname="./data/SimHei.ttf", size=14)  

          # 數(shù)據(jù)準(zhǔn)備
          labels=np.array(["英語","數(shù)學(xué)","語文","化學(xué)","物理","體育"])
          stats=[908076707588]

          # 畫圖數(shù)據(jù)準(zhǔn)備,角度、狀態(tài)值
          angles=np.linspace(02*np.pi, len(labels), endpoint=False)
          stats=np.concatenate((stats,[stats[0]]))
          angles=np.concatenate((angles,[angles[0]]))

          # 畫蜘蛛圖
          fig = plt.figure(figsize = (10,6))
          ax = fig.add_subplot(111, polar=True)   
          ax.plot(angles, stats, 'o-', linewidth=2)
          ax.fill(angles, stats, alpha=0.25)

          # 設(shè)置中文標(biāo)題和維度名稱
          ax.set_thetagrids(angles * 180/np.pi, labels, FontProperties=font)
          ax.set_title("小明各科成績",FontProperties = font, size = 20)
          plt.show()

          二,將中文字體文件放入matplotlib安裝目錄下

          這種方法步驟較多,但是只要初始設(shè)置了中文字體后,此后用到地方無需再特別指定字體參數(shù)。

          step1:下載中文字體文件 SimHei.ttf

          在公眾號算法美食屋后臺回復(fù)關(guān)鍵字:中文字體,可以獲取SimHei中文字體。

          step2:找到matplotlib的安裝路徑

          !pip show matplotlib 
          Name: matplotlib
          Version: 3.2.1
          Summary: Python plotting package
          Home-page: https://matplotlib.org
          Author: John D. Hunter, Michael Droettboom
          Author-email: [email protected]
          License: PSF
          Location: /Users/liangyun/anaconda3/lib/python3.7/site-packages
          Requires: pyparsing, cycler, python-dateutil, kiwisolver, numpy
          Required-by: seaborn, scikit-image, pycocotools, pandas-alive, gopup, akshare, bar-chart-race

          于是matplotlib的安裝路徑是 /Users/liangyun/anaconda3/lib/python3.7/site-packages

          step3:將字體文件放入到matplotlib安裝目錄下的字體文件夾下

          !mv SimHei.ttf /Users/liangyun/anaconda3/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf/

          step4:找到maplotlib的緩存位置

          import matplotlib as mpl 
          mpl.get_cachedir()

          于是找到maptlotlib的緩存位置是 /Users/liangyun/.matplotlib

          step5:清空緩存

          !rm -rf /Users/liangyun/.matplotlib

          step6: 代碼開始處設(shè)置字體

          import matplotlib.pyplot as plt
          import seaborn as sns

          plt.rcParams['font.family'] = ['sans-serif']
          plt.rcParams['font.size'] = '20'
          plt.rcParams['font.sans-serif'] = ['SimHei']


          labels=np.array(["英語","數(shù)學(xué)","語文","化學(xué)","物理","體育"])
          names = ["李雷","韓梅梅","湯姆","安"]
          scores=np.array([[908076707588],[706073809555],
                           [706056306595],[504066757498]])

          fig = plt.figure(0,figsize = (10,6))
          plt.matshow(scores,fignum = 0)

          plt.xticks(ticks = range(len(labels)),labels = labels)
          plt.yticks(ticks = range(len(names)),labels = names)

          # 繪制?本
          for i in range(len(names)):
              for j in range(len(labels)):
                  plt.text(j, i, round(scores[i, j],1), ha="center", va="center", color='r')
          plt.colorbar()
          plt.show()

          瀏覽 81
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产毛片儿 | 日韩乱伦中文字幕 | 日韩A∨ | 伊人国产综合视频在线 | 射一射在线视频 |