[Solved] Python matplolib mac 電腦中文字不能顯示

2023/07/31閱讀時間約 2 分鐘

直接來看無法顯示的狀態

raw-image

Error msg:

UserWarning: Glyph 39006 (\N{CJK UNIFIED IDEOGRAPH-985E}) missing from current font. fig.canvas.print_figure(bytes_io, **kw)

UserWarning: Glyph 37327 (\N{CJK UNIFIED IDEOGRAPH-91CF}) missing from current font. fig.canvas.print_figure(bytes_io, **kw)

Output is truncated. View as a [scrollable element](command:cellOutput.enableScrolling?44759503-8355-41d4-aad1-d64a9ecd7a1e) or open in a [text editor](command:workbench.action.openLargeOutput?44759503-8355-41d4-aad1-d64a9ecd7a1e). Adjust cell output [settings](command:workbench.action.openSettings?%5B%22%40tag%3AnotebookOutputLayout%22%5D)...


我們透過 font_manager 查看目前有安裝哪些字體

from matplotlib.font_manager import FontManager
fm = FontManager()
mat_fonts = set(f.name for f in fm.ttflist)
print(mat_fonts)


在輸出影像時修改,指定已安裝的字體, mac 大部分都有Arial Unicode Ms

import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Arial Unicode Ms']


可以用這個測試

plt.figure(figsize=(10, 6))
plt.bar(['A', 'B', 'C'], [10, 20, 15])
plt.title('中文字測試', fontsize=16) # 標題中應用中文字體
plt.xlabel('類別', fontsize=12) # 標籤中應用中文字體
plt.ylabel('數量', fontsize=12) # 標籤中應用中文字體
plt.show()
raw-image

成功了!

23會員
28內容數
歡迎來到《桃花源記》專欄。這裡不僅是一個文字的集合,更是一個探索、夢想和自我發現的空間。在這個專欄中,我們將一同走進那些隱藏在日常生活中的"桃花源"——那些讓我們心動、讓我們反思、讓我們找到內心平靜的時刻和地方
留言0
查看全部
發表第一個留言支持創作者!