您的位置:首页 > 编程语言 > PHP开发

matplotlib命令与格式:参数配置文件与参数配置

2017-11-28 10:41 537 查看
转载:https://my.oschina.net/swuly302/blog/94805

1.matplotlib参数配置文件

matplotlib使用matplotlibrc [matplotlib resource configurations]配置文件来自定义各种属性,我们称之为rc配置或者rc参数。在matplotlib中你可以控制几乎所有的默认属性:视图窗口大小以及每英寸点数[dpi],线条宽度,颜色和样式,坐标轴,坐标和网格属性,文本,字体等属性。

matplotlib将默认参数配置保存在“matplotlibrc”文件中,通过修改配置文件,可修改图表的缺省样式。

在matplotlib中可以使用多个“matplotlibrc”配置文件,它们的搜索顺序如下,顺序靠前的配置文件将会被优先采用。

当前路径:程序的当前路径。
用户配置路径:在用户文件夹的“.matplotlib”目录下,可通过环境变量MATPLOTLIBRC修改它的位置。
系统配置路径:保存在matplotlib的安装目录下的mpl-data中。

通过下面的语句可以获取用户配置路径:

>>> import matplotlib

>>> matplotlib.get_configdir()

通过下面的语句可以获得目前使用的配置文件的路径:

>>> import matplotlib

>>> matplotlib.matplotlib_fname()

通过下面的语句可以读取的配置文件中所有参数及其参数值

在matplotlib模块载入时会调用rc_params(),并把得到的配置字典保存到rcParams变量中:

>>> matplotlib.rc_params()

{'agg.path.chunksize': 0,

 'axes.axisbelow': False,

 'axes.edgecolor': 'k',

 'axes.facecolor': 'w',

 ... ...}

2.matplotlib参数读取与配置

(1)读取matplotlib默认配置参数
import matplotlib as mpl
print(mpl.rcParams)

(2)设置默认参数方法
可以通过matplotlib.rcParams配置各matplotlib命令的默认参数,如下配置线条默认大小与颜色:
import matplotlib as mplmpl.rcParams['lines.linewidth'] = 2mpl.rcParams['lines.color'] = 'r'

也可以通过matplotlib .rc一次设置多个参数
import matplotlib as mplmpl.rc('lines', linewidth=2, color='r')

(3)rcParams恢复缺省的配置
如果希望恢复到缺省的配置(matplotlib载入时从配置文件读入的配置),可以调用rcdefaults()。
>>> matplotlib.rcdefaults()

如果手工修改了配置文件,希望重新从配置文件载入最新的配置,可以调用:
>>> matplotlib.rcParams.update( matplotlib.rc_params() )
通过pyplot模块也可以使用rcParams、rc和rcdefaults。

3.matplotlib所有参数项

### 线条

# 更多线条属性详情请查看

# http://matplotlib.sourceforge.net/api/artist_api.html#module-matplotlib.lines 
#lines.linewidth : 1.0 # 线宽

#lines.linwstyle : - # 实线

#lines.color : blue

#lines.marker : None # 默认标记

#lines.markeredgewidth : 0.5 # 在标记附近的线宽

#lines.markersize : 6 # 标记大小

#lines.dash_joinstyle : miter # miter|round|bevel

#lines.dash_capstyle : butt # butt|round|projecting

#lines.solid_joinstyle : miter # miter|round|bevel

#lines.solid_capstyle : projecting # butt|round|projecting

#lines.antialiased : True # 使用抗锯齿渲染(没有缺口)

### 斑纹[PATCHES]

# 斑纹是图形对象,用来填充2D空间,如多边形或者圆

# 更多详情请查看 http://matplotlib.sourceforge.net/api/artist_api.html#module-matplotlib.patches
#patch.linewidth : 1.0 # edge width in points

#patch.facecolor : blue

#patch.edgecolor : black

#patch.antialiased : True # 使用抗锯齿渲染(没有缺口)

### 字体



# 字体属性被text.Text所使用。更多详情请查看http://matplotlib.sourceforge.net/api/font_manager_api.html

# 下面给出6种带默认值的字体属性

# font.family属性有五个值: "serif"(如:Times),

# "sans-serif"(如:Helvetical), "cursive"(如:Zapf-Chancery)

# "fantasy"(如:Western)和"monospace"(如: Courier).

# 每个字体家族都有默认一些列以优先级下降的顺序排列的字体

#

# font.style属性有三个值: normal(或者roman), italic,或者oblique. 

# oblique风格如果没有出现,它会在italic中使用。

#

# font.variant属性有2个值: normal或者small-caps.对于TrueType类型的字体,

# 可缩放字形,大写字母的小写形式(如:ABCDE)和使用‘smaller’字体大小或者当前字体大小的83%左右是等价的

#

# font.weight属性有13个有效值:normal, bold, bolder, lighter, 100, 200, 300, ..., 900.

# Normal和400,bold和700是一样的,而bolder和lighter是针对当前高度的相对大小

#

# font.stretch属性有11个值: ultra-condensed,

# extra-condensed, condensed, semi-condensed, normal, semi-expanded,

# expanded, extra-expanded, ultra-expanded, wider, and narrower.

# 这个属性迄今还没有实现。

#

# font.size属性是为文本设置默认字体大小,以磅为单位,12pt是标准值。

#

#font.family : sans-serif

#font.style : normal

#font.variant : normal

#font.weight : medium

#font.stretch : normal

# 注意font.size控制着默认字体大小。如果想要配置指定文本的大小如tick标签,坐标轴,标签,标题等等。

# 请看坐标轴和ticks的rc配置。指定文本大小可以使用下面的值:xx-small, x-small,small, medium, 

# large, x-large, xx-large, larger,或者smaller来相对font.size大小定义

#fo
cfba
nt.size : 12.0

#font.serif : Bitstream Vera Serif, New Century Schoolbook, Century Schoolbook L, Utopia, ITC Bookman, Bookman, Nimbus Roman No9 L, Times New Roman, Times, Palatino, Charter, serif

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

#font.cursive : Apple Chancery, Textile, Zapf Chancery, Sand, cursive

#font.fantasy : Comic Sans MS, Chicago, Charcoal, Impact, Western, fantasy

#font.monospace : Bitstream Vera Sans Mono, Andale Mono, Nimbus Mono L, Courier New, Courier, Fixed, Terminal, monospace

### 文本

#

# 文本属性被text.Text使用。更多详情请查看

# http://matplotlib.sourceforge.net/api/artist_api.html#module-matplotlib.text 
#text.color : black

### LaTex自定义

# 查看http://www.scipy.org/Wiki/Cookbook/Matplotlib/UsingTex

#text.usetex : False # 所有文本都用latex来处理。可以通过rc参数设置来设置下面支持的字体:

# new century schoolbook, bookman, times, palatino,

# zapf chancery, charter, serif, sans-serif, helvetica,

# avant garde, courier, monospace, 

# computer modern roman, 

# computer modern sans serif, computer modern typewriter 

# 如果LaTeX的\userpackage命令需要其它的字 

# 体,请在matplotlib邮件列表里查询  

#text.latex.unicode : False # 使用"ucs"和"inputenc" LaTeX包来处理unicode字符串

#text.latex.preamble : # 不正确的使用这个特性会导致LaTeX错误和不支持。

# 如果这个特性不能按你期望的工作,请不要求救 

# preamble是逗号分隔的列表的LaTeX语句,它包含在LaTeX的preamble文档 

# 如: 

# text.latex.preamble : \usepackage{bm},\usepackage{euler}

# 这个包总是会随这usetex而载入。所以请小心包冲突:color, geometry,  

# graphicx, type1cm, textcomp. 根据你的字体设置Adobe的Postscript  

# font可能会载入。  

#text.dvipnghack : None # dvipng的一些版本不能正确的处理alpha通道。 

# 请在测试之前设置为True来检测和刷新 

#  ~/.matplotlib/tex.cache。设置为False来关闭检测。 

# 设置为None会根据你dvipng版本检测和猜测  

#text.hinting : True # 如果为True,文本就会是提示文本,否则,就不是提示文本。它只对Agg后端其作用

#text.antialiased : True # 如果为True(默认值),文本就会抗锯齿。它只对Agg后端其作用

# 下面的设置允许你选择数学模式的字体

# 它们从TeX字体映射到字体配置模式。这些设置只有在mathtext.fontset设置为'custom'时才会被使用。

# 注意的是这个'custom'模式现在并不被支持,可能在不久会被遗弃。

#mathtext.cal : cursive

#mathtext.rm : serif

#mathtext.tt : monospace

#mathtext.it : serif:italic

#mathtext.bf : serif:bold

#mathtext.sf : sans

#mathtext.fontset : cm # 应该为 'cm' (Computer Modern), 'stix',

# 'stixsans' or 'custom'

#mathtext.fallback_to_cm : True # When True, use symbols from the Computer Modern

# fonts when a symbol can not be found in one of

# the custom math fonts.

#mathtext.default : it # 数学默认字体。可以是任何LaTeX字体名包括一些专门的合法字体

### 坐标轴

# 默认面和边颜色以及tick大小

# tick标签的默认字体大小等等。

# 请查看:

# http://matplotlib.sourceforge.net/api/axes_api.html#module-matplotlib.axes
#axes.hold : True # 是否清除坐标轴,默认是开启的

#axes.facecolor : white # 坐标轴的背景颜色

#axes.edgecolor : black # 坐标轴边的颜色

#axes.linewidth : 1.0 # 边的宽

#axes.grid : False # 是否显示网格

#axes.titlesize : large # 坐标轴标题字体的大小

#axes.labelsize : medium # x轴和y轴的字体大小

#axes.labelweight : normal # x轴和y轴的字体高度

#axes.labelcolor : black

#axes.axisbelow : False # 坐标轴网格线以及ticks是否在坐标轴的线条或者文本等元素下面显示

#axes.formatter.limits : -7, 7 # use scientific notation if log10

# of the axis range is smaller than the

# first or larger than the second

#axes.formatter.use_locale : False # 当为真时,会根据用户的本地环境格式化tick标签。

# 例如:当使用','分隔时,在法国会当作小数分割号 

#axes.unicode_minus : True # 减号使用unicode编码而不是连字符号,请查看

# http://en.wikipedia.org/wiki/Plus_and_minus_signs#Character_codes
#axes.color_cycle : b, g, r, c, m, y, k # 绘制线条的颜色循环,颜色列表:单个字母或者全名,或者web风格的十六进制。

#polaraxes.grid : True # 极坐标轴显示网格

#axes3d.grid : True # 3d坐标轴显示网格

### 刻度/记号[TICKS]

# 查看 http://matplotlib.sourceforge.net/api/axis_api.html#matplotlib.axis.Tick
#xtick.major.size : 4 # 最大刻度大小

#xtick.minor.size : 2 # 最小刻度大小

#xtick.major.width : 0.5 # 最大刻度宽度

#xtick.minor.width : 0.5 # 最小刻度宽度

#xtick.major.pad : 4 # 最大刻度标签距离

#xtick.minor.pad : 4 # 最小刻度标签距离

#xtick.color : k # 刻度标签颜色

#xtick.labelsize : medium # 刻度标签字体大小

#xtick.direction : in # 指向: in or out

#ytick.major.size : 4 # 最大刻度大小

#ytick.minor.size : 2 # 最小刻度大小

#ytick.major.width : 0.5 # 最大刻度宽度

#ytick.minor.width : 0.5 # 最小刻度宽度

#ytick.major.pad : 4 # 最大刻度标签距离

#ytick.minor.pad : 4 # 最小刻度标签距离

#ytick.color : k # 刻度标签颜色

#ytick.labelsize : medium # 刻度标签字体大小

#ytick.direction : in # 指向: in or out

### 网格[GRIDS]

#grid.color : black # 网格颜色

#grid.linestyle : : # 点

#grid.linewidth : 0.5 # pt

### 图例[Legend]

#legend.fancybox : False # 为True时使用圆角方框,否则使用直角方框

#legend.isaxes : True

#legend.numpoints : 2 # the number of points in the legend line

#legend.fontsize : large

#legend.pad : 0.0 # 已弃用了

#legend.borderpad : 0.5 # 字体大小单元的边缘的空白

#legend.markerscale : 1.0 # the relative size of legend markers vs. original

# the following dimensions are in axes coords 

#legend.labelsep : 0.010 # 已弃用了 

#legend.labelspacing : 0.5 # 图例之间的竖直距离,使用字体大小的几分之几表示 

#legend.handlelen : 0.05 # 已弃用了 

#legend.handlelength : 2. # 图例之间的长度,使用字体大小的几分之几表示 

#legend.handleheight : 0.7 # 图例之间的高度,使用字体大小的几分之几表示 

#legend.handletextsep : 0.02 # 已弃用了 

#legend.handletextpad : 0.8 # 图例的图例线和图例文本之间的距离,使用字体大小的几分之几表示 

#legend.axespad : 0.02 # 已弃用了 

#legend.borderaxespad : 0.5 # 图例边缘和坐标轴之间的空隙距离,使用字体大小的几分之几表示 

#legend.columnspacing : 2. #  

#legend.shadow : False 

#legend.frameon : True # 是否在图例外显示外框 

### 视图窗口[FIGURE]

# 请查看 http://matplotlib.sourceforge.net/api/figure_api.html#matplotlib.figure.Figure
#figure.figsize : 8, 6 # 视图窗口大小,英寸表示

#figure.dpi : 80 # 视图窗口 每英寸点数

#figure.facecolor : 0.75 # 视图窗口颜色; 0.75是使用灰度值

#figure.edgecolor : white # 视图窗口边的颜色

# 视图窗口的子视图参数. 所有的大小都是视图窗口大小的几分之几

#figure.subplot.left : 0.125 # 左部子视图

#figure.subplot.right : 0.9 # 右部子视图

#figure.subplot.bottom : 0.1 # 下部子视图

#figure.subplot.top : 0.9 # 上部子视图

#figure.subplot.wspace : 0.2 # 子视图之间的横向空白间距

#figure.subplot.hspace : 0.2 # 子视图之间的纵向空白间距

### 图像[IMAGES]

#image.aspect : equal # equal | auto | a number

#image.interpolation : bilinear # 使用help(imshow)获得更多

#image.cmap : jet # gray | jet etc...

#image.lut : 256 # 色彩对照表查找大小

#image.origin : upper # lower | upper

#image.resample : False

### 轮廓图[CONTOUR PLOTS]

#contour.negative_linestyle : dashed # dashed | solid

### Agg 渲染

### 警告: 还在实验中, 2008/10/10

#agg.path.chunksize : 0 # 0为禁用;取值在10000到100000可以约微提高速度和减少Agg渲染失败当绘制很大数据时。

# 尽管它可能会产生假象。20000是一个很好初始点。 

### 保存视图窗口

#path.simplify : True # 当为True时,通过删除不可见的点来减少文件的大小和提供渲染速度来简单化

#path.simplify_threshold : 0.1 # 在简单化过程中,在相似度的阙值下面的至高点将会删除

#path.snap : True # When True, rectilinear axis-aligned paths will be snapped to

# the nearest pixel when certain criteria are met. When False,

# paths will never be snapped.

# 默认的保存视图窗口的参数会因为显示参数的不同而不同。如:你想高分辨率,会使视图窗口的背景为白色。

#savefig.dpi : 100 # 视图窗口 每英寸点数

#savefig.facecolor : white # 视图窗口保存时颜色

#savefig.edgecolor : white # 视图窗口边在保存时的颜色

#savefig.extension : auto # 使用什么文件后缀

#cairo.format : png # png, ps, pdf, svg

# tk 后端参数

#tk.window_focus : False # Maintain shell focus for TkAgg

# ps 后端参数

#ps.papersize : letter # auto, letter, legal, ledger, A0-A10, B0-B10

#ps.useafm : False # use of afm fonts, results in small files

#ps.usedistiller : False # can be: None, ghostscript or xpdf

# Experimental: may produce smaller files.

# xpdf intended for production of publication quality files,

# but requires ghostscript, xpdf and ps2eps

#ps.distiller.res : 6000 # dpi

#ps.fonttype : 3 # Output Type 3 (Type3) or Type 42 (TrueType)

# pdf 后端参数

#pdf.compression : 6 # integer from 0 to 9

# 0 disables compression (good for debugging)

#pdf.fonttype : 3 # Output Type 3 (Type3) or Type 42 (TrueType)

# svg 后端参数

#svg.image_inline : True # 直接将光栅图数据写入到svg文件中

#svg.image_noscale : False # 在svg中不缩放光栅数据比例

#svg.fonttype : 'path' # 怎么处理svg的字体

# 'none': 假设字体已经安装了,svg可以正常查看

# 'path': 在路径中植入字符--支持大多数的SVG渲染器

# 'svgfont': 植入字符作为SVG的字体--被Chrome,Opera和Safari支持

# docstring 参数

#docstring.hardcopy = False # 如果你想生成硬拷贝的文档就设置它

# 设置冗长信息标志。 它会控制在matplotlib在运行时可以给你多少信息。

# 冗长信息的等级可以是: silent, helpful, debug, debug-annoying.任何等级都会包含它前面的等级。

# 如你设置的等级为debug,那么你会获得所有debug和helpful等级的信息。

# 如果使用邮件列表提交问题时,请设置为helpful或者debug,并且将输出附加到你的报告中。

#

# fileo告诉冗余信息报告输出的目的地。可以是一个文件名,或者一个文件句柄像sys.stdout.

# 你可以重载rc默认冗余信息,在命令行中使用标志--vervbose-LEVEL,LEVEL是合法的等级如:--verbose-helpful

#

# 你可以在你的代码中访问冗余信息的实例。

# from matplotlib import verbose.

#verbose.level : silent # one of silent, helpful, debug, debug-annoying

#verbose.fileo : sys.stdout # a log filename, sys.stdout or sys.stderr

# 通过键盘和视图窗口交互的事件键

# 根据你的要求,自定义这些设置

# 如果你不需要键对照表,请空着(如: fullscreen : '')

#keymap.fullscreen : f # 全屏

#keymap.home : h, r, home # 主页或者重置助记符

#keymap.back : left, c, backspace # 启用前进或者后退键

#keymap.forward : right, v # 左手快速导航

#keymap.pan : p # 移动助记符

#keymap.zoom : o # 缩放助记符

#keymap.save : s # 保存当前视图窗口

#keymap.grid : g # 当前坐标轴的网格开关

#keymap.yscale : l # y轴缩放(对数/线性)

#keymap.xscale : L, k # x轴缩放(对数/线性)

#keymap.all_axes : a # 启动所有的坐标轴
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息