Solve matplotlib, serborn Paint garbled phenomenon
Develop fonts Paint
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=12)
plt.plot([1,2,3])
plt.title(u"测试",fontproperties=font)
This method is cumbersome, each time drawing, must specify the font location. The following describes a method for once and for all.
- Modify the configuration information
- The C: \ Windows \ Fonts The following font simsun.ttf, Microsoft elegant black font copied to D: \ Programs \ Anaconda \ Lib \ site-packages \ matplotlib \ mpl-data \ fonts \ folder under ttf. (Anaconda and the installation location of the folder)
- Find the following two lines:
#font.family : sans-serif
#font.sans-serif : Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
ont.family : sans-serif
font.sans-serif : SimHei,Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
Restart python, matplotlib can output the Chinese characters.
seaborn font problems
The above method can be used for matplotlib, but if introduced seaborn, it will be garbled, methods are now finding out that adding the following code in a program
import seaborn as sns
sns.set_style('whitegrid',{'font.sans-serif':['simhei','Arial']})
Garbage problem mac os x system
import matplotlib.pyplot as plt
import seaborn as sns
plt.rcParams['font.family'] = ['Arial Unicode MS'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False #用来正常显示负号
sns.set_style('whitegrid',{'font.sans-serif':['Arial Unicode MS','Arial']})