Python利用Matplotlib绘图无法显示中文字体的解决方案
生活随笔
收集整理的這篇文章主要介紹了
Python利用Matplotlib绘图无法显示中文字体的解决方案
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
這里寫(xiě)目錄標(biāo)題
- 問(wèn)題描述
- 報(bào)錯(cuò)信息
- 解決方法
- 其他解決方案
- 使用模板(內(nèi)置樣式)后無(wú)法顯示中文的解決方案
問(wèn)題描述
在Python利用Matplotlib繪圖的時(shí)候,無(wú)法顯示坐標(biāo)軸上面的中文和標(biāo)題里面的中文
import matplotlib.pyplot as plt from numpy import squaresquares = [1, 4, 9, 16, 25] fig, ax = plt.subplots() ax.plot(squares, linewidth = 3)# 設(shè)置圖表標(biāo)題并給坐標(biāo)軸加上標(biāo)簽。 ax.set_title("平方數(shù)", fontsize = 24) ax.set_xlabel("值", fontsize = 14) ax.set_ylabel("值的平方", fontsize = 14)# 設(shè)置刻度標(biāo)記的大小。 ax.tick_params(axis='both', labelsize =14 )plt.show()運(yùn)行后如下界面
報(bào)錯(cuò)信息
A:\ProgramData\Anaconda3\envs\py39\lib\tkinter\__init__.py:814: UserWarning: Glyph 24179 (\N{CJK UNIFIED IDEOGRAPH-5E73}) missing from current font. func(*args)
解決方法
在開(kāi)頭插入
import matplotlib matplotlib.rc("font", family='Microsoft YaHei')這兩行代碼將圖片中的字體設(shè)置為了Microsoft YaHei,所以可以正常顯示了
import matplotlib.pyplot as pltimport matplotlib matplotlib.rc("font", family='Microsoft YaHei') from numpy import squaresquares = [1, 4, 9, 16, 25] fig, ax = plt.subplots() ax.plot(squares, linewidth = 3)# 設(shè)置圖表標(biāo)題并給坐標(biāo)軸加上標(biāo)簽。 ax.set_title("平方數(shù)", fontsize = 24) ax.set_xlabel("值", fontsize = 14) ax.set_ylabel("值的平方", fontsize = 14)# 設(shè)置刻度標(biāo)記的大小。 ax.tick_params(axis='both', labelsize =14 )plt.show()其他解決方案
import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = 'SimHei' # 黑體使用模板(內(nèi)置樣式)后無(wú)法顯示中文的解決方案
在模板代碼后面加一行 plt.rcParams[‘font.sans-serif’] = ‘SimHei’
input_values = [1, 2, 3, 4, 5] squares = [1, 4, 9, 16, 25]plt.style.use('seaborn') plt.rcParams['font.sans-serif'] = 'SimHei' fig, ax = plt.subplots() ax.plot(input_values, squares, linewidth = 3)SimSun :宋體;KaiTI:楷體;Microsoft YaHei:微軟雅黑
LiSu:隸書(shū);FangSong:仿宋;Apple LiGothic Medium:蘋(píng)果麗中黑;
總結(jié)
以上是生活随笔為你收集整理的Python利用Matplotlib绘图无法显示中文字体的解决方案的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: rsync来实现文件同步
- 下一篇: Redmi 游戏电视 X Pro 评测