Matplotlib 中文字体显示

1. 一般在C:\Windows\Fonts 里可以找到Mircosoft YaHei UI字体文件


2. 将字体文件拷贝至下面的目录中

python安装目录\lib\site-packages\matplotlib\mpl-data\fonts\ttf

3. 修改matplotlibrc文件,使用python查看文件路径

>>> import matplotlib
>>> matplotlib.matplotlib_fname()
'C:\\Users\\username\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages\\matplotlib\\mpl-data\\matplotlibrc'

打开matplotlibrc文件,

(1)删除 font.family 前面的 并将冒号后面改为“Microsoft YaHei”;

(2)删除font.sans-serif 前面的 并在冒号后面添加“Microsoft YaHei

194 font.family         : Microsoft YaHei
195 #font.style          : normal
196 #font.variant        : normal
197 #font.weight         : normal
198 #font.stretch        : normal
206 font.sans-serif     : Microsoft YaHei, DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
207 #font.cursive        : Apple Chancery, Textile, Zapf Chancery, Sand, Script MT, Felipa, cursive
208 #font.fantasy        : Comic Sans MS, Chicago, Charcoal, ImpactWestern, Humor Sans, xkcd, fantasy
209 #font.monospace      : DejaVu Sans Mono, Bitstream Vera Sans Mono, Computer Modern Typewriter, Andale Mono, Nimbus Mono L, Courier New, Courier, Fixed, Terminal, monospace


修改后保存,重新运行程序。


猜你喜欢

转载自blog.csdn.net/css_aaa/article/details/80853800