用matplotlib画图有一段时间了,这次把一些自己的心得体会做个总结:
1.如何在python脚本和python shell 动态修改matplotlib画图的参数。
matplotlib.rcParams: 存储所有rc设置的类似字典的变量。
import matplotlib as mpl
mpl.rcParams['lines.linewidth'] = 2
mpl.rcParams['lines.color'] = 'r'
也可以使用
matplotlib.rc()命令一次性修改同组的多个参数。组名类似: lines,patches,font,text等等。
import matplotlib as mpl
mpl.rc('lines', linewidth=2, color='r')
2.如何修改matplotlib画图的默认参数,即在python脚本中不用进行动态修改。
matplotlib 使用matplotlibrc 设置文件去定制所有参数。首先,你要找到matplotlibrc文件。
1)现有的工作目录下
2)$MATPLOTLIBRC/matplotlibrc
3)~/.config/matplotlib/matplotlibrc
4)INSTALL/matplotlib/mpl-data/matplotlibrc,INSTALL 一般来说指usr/lib/python2.7/site-packages on Linux
实在找不到的话,使用shell命令find / -name matplotlibrc 2>/dev/null. 将找到的matplotlibrc拷贝到~/.config/matplotlib/然后按照自己的需要进行设置。
也可以用下面的函数去查看加载matplotlibrc的路径
>>> import matplotlib
>>> matplotlib.matplotlib_fname()
'/home/foo/.config/matplotlib/matplotlibrc'