Matplotlib 中文標籤問題
作者:王一哲
日期:2020/3/2
這篇只能算是按照網路上找到的方法,在自己的電腦上測試的筆記,主要參考以下兩篇文章:
- 解决Python使用matplotlib绘图时出现的中文乱码问题
- PYTHON 如何在Win 10解決matplotlib中文顯示的問題?
Windows 10
如果電腦裡有安裝中易宋體 (SimSun) 或是 中易黑體 (SimHei),可以按照第一篇文章的作法,直接在程式碼裡設定
或是
但如果沒有安裝對應的字體,則要按照第二篇文章的作法,先安裝思源宋體,這是下載字體的連結。再到下列的檔案裡找到對應的字體名稱
例如我使用的是 NotoSansCJKtc 系列的字體,名稱是 Noto Sans CJK TC。以下是我試著使用中文標籤的程式碼,測試的環境為 Windows 10 家用版、Python 3.7.5、matplotlib 3.1.1。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
於 Windows 10 測試結果
Linux
測試的環境為 Lubuntu 19.10、Python 3.7.5、matplotlib 3.1.3。方法同上,先將下載回來的字體複製到以下的資料夾中
再移除以下的檔案
如果擔心會出問題,可以先複製一份,如果失敗了再將檔案蓋回去就好。開啟 Python IDLE,執行前面的程式碼,可以畫出同樣的圖形,測試成功。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
於 Lubuntu 19.10 測試結果