Modify matplotlib parameters in anaconda

 

First introduce my development environment:

   System platform: mac OS 10.15
    anaconda 3:
    matplotlib 2.0.2

There are three schemes for modifying the default configuration of the chart drawn by matplotlib (take Chinese support as an example):    

1. Solution 1: Specify the Chinese font library installed on this machine through font_manager. Please make sure that this font library (fm.FontProperties(fname='/Library/Fonts/Arial Unicode.ttf') is installed in the corresponding path of the machine. 

#中文文本的解决方案:
#matplot显示图例中的中文问题 :   https://www.zhihu.com/question/25404709/answer/67672003
import matplotlib.font_manager as fm
#mac中的字体问题请看: https://zhidao.baidu.com/question/161361596.html
#  或  /System/Library/Fonts 
myfont = fm.FontProperties(fname='/Library/Fonts/Arial Unicode.ttf')  #  C:\Windows\Fonts\simsun
plt.axis([0,5,0,20])
plt.title('我的第一个图表',fontsize=20,fontname='Times New Roman',fontproperties=myfont)  # 要便用  fontproperties来指定中文字体
plt.xlabel('计数',fontproperties=myfont)
plt.ylabel('平方值',fontproperties=myfont)
plt.text(1,1.5,'一',fontproperties=myfont)
plt.text(2,4.5,'二',fontproperties=myfont)
plt.text(3,9.5,'三',fontproperties=myfont)
plt.text(4,16.5,'四')
plt.plot([1,2,3,4],[1,4,9,16],'ro')
plt.show()

2. Scheme 2: By specifying plt.style.use(<style-name>) or plt.rcParams configuration style, it will have an effect on all the graphs drawn by this plt. For the documentation of these two schemes, please refer to:   https ://matplotlib.org/3.3.0/tutorials/introductory/customizing.html?highlight=text.usetex#

 For specific configuration items, please refer to the file matplotlibrc in the installation directory of matplotlib. The location of this file can be viewed with the following command:   

>>> import matplotlib
>>> matplotlib.matplotlib_fname()
'/home/foo/.config/matplotlib/matplotlibrc'

The specific content of this file can also be viewed on matplotlib's official website at:  https://matplotlib.org/3.3.0/tutorials/introductory/customizing.html?highlight=text.usetex#matplotlibrc-sample

The following is my test code: 

plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
plt.rcParams['axes.grid']=True
#以上其它的配置项可以参考官方文档


#rcParams可以指定绘图中所有的外观配置,具体的参数请参见: 
plt.axis([0,5,0,20])
plt.title('我的第一个图表',fontsize=20)  # 要便用  fontproperties来指定中文字体
plt.xlabel('计数')
plt.ylabel('平方值')
plt.text(1,1.5,'一')
plt.text(2,4.5,'二')
plt.text(3,9.5,'三')
plt.text(4,16.5,'四')
plt.plot([1,2,3,4],[1,4,9,16],'ro')
plt.show()

Analysis There are five types of font families:

#font.serif:      DejaVu Serif, Bitstream Vera Serif, Computer Modern Roman, New Century Schoolbook, Century Schoolbook L, Utopia, ITC Bookman, Bookman, Nimbus Roman No9 L, Times New Roman, Times, Palatino, Charter, serif
#font.sans-serif: DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
#font.cursive:    Apple Chancery, Textile, Zapf Chancery, Sand, Script MT, Felipa, cursive
#font.fantasy:    Comic Neue, Comic Sans MS, Chicago, Charcoal, ImpactWestern, Humor Sans, xkcd, fantasy
#font.monospace:  DejaVu Sans Mono, Bitstream Vera Sans Mono, Computer Modern Typewriter, Andale Mono, Nimbus Mono L, Courier New, Courier, Fixed, Terminal, monospace

By default, there is no Chinese font. Here I installed a SimHei Chinese font into the mac, which belongs to the sans-serif font family. So I configured

plt.rcParams['font.sans-serif']=['SimHei'] #Used to display Chinese labels normally,

In this way, Chinese can be displayed, but this configuration scheme is only valid for the current plt, and another process is invalid. 

 

3. Solution 3: Realize global configuration by modifying the matplotlibrc file. 

   This file is in my location: /Applications/anocadote/anaconda/lib/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc

   Find one of them

#font.sans-serif: DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif

 changed to:

font.sans-serif     : SimHei

 Please note: All the parts beginning with # in this file are the default configuration of the system. You can refer to some basic configuration of matplotlib. 

  Verify that the above global configuration is successful:

import matplotlib
print(matplotlib.rc_params()) 

The following results can be obtained, indicating that the configuration is successful:

Guess you like

Origin blog.csdn.net/zhangyingchengqi/article/details/107911695