自定義matplotlib

自定義matplotlib

Created Saturday 08 December 2012javascript

5.1 matplotlibrc文件

matplotlib使用matplotlibrc [matplotlib resource configurations]配置文件來自定義各類屬性,咱們稱之爲rc配置或者rc參數。在matplotlib中你能夠控制幾乎全部的默認屬性:視圖窗口大小以及每英寸點數[dpi],線條寬度,顏色和樣式,座標軸,座標和網格屬性,文本,字體等屬性。matplotlib從下面的3個地方按順序查找matplotlibrc文件:php

1. 當前工做目錄下的matplotlibrc,一般用在你不想被其它地方使用的特定自定義[customizations] 

2. 用戶默認的自定義在.matplotlib/matplotlibrc裏。查看  .matplotlib目錄存放位置 
html

3. python安裝目錄/matplotlib/mpl-data/matplotlibrc.若是是linux,那麼python安裝目錄像這樣:/usr/lib/python2.5/site-packages。若是在windows,多是c:\pytho2.5\lib\site-packages。每次你安裝matplotlib,這個文件就會被覆蓋。若是你想要你的自定義配置保存下來,請把這個文件放到你的.matplotlib目錄下。 

想查看當前工做的matplotlibrc文件是哪一個,你可使用下面的方式查看:java

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

請看下面matplotlibrc文件例子python

5.2 rc動態配置

你能夠在python腳本或者python交互式環境裏動態的改變默認rc配置。全部的rc配置變量稱爲matplotlib.rcParams 使用字典格式存儲,它在matplotlib中是全局可見的。rcParams能夠直接修改,如:linux

import matplotlib as mpl mpl.rcParams['lines.linewidth'] = 2 mpl.rcParams['lines.color'] = 'r'

Matplotlib還提供了一些便利函數來修改rc配置。matplotlib.rc()命令利用關鍵字參數來一次性修改一個屬性的多個設置:git

import matplotlib as mpl mpl.rc('lines', linewidth=2, color='r')


這裏matplotlib.rcdefaults()命令能夠恢復爲matplotlib標準默認配置。 
還有能夠驗證設置rcParams的值,詳情查看matplotlib.rcsetup。
github

5.2.1 一個matplotlibrc文件示例

### MATPLOTLIBRC 格式web

# 這是一個matplotlib簡單配置文件-你能夠從你的系統下的site-packages/matplotlib/mpl-data/matplotlibrc找到。
# 若是你要修改你,請注意它會在你下次安裝時覆蓋。若是你想要長久保持不被覆蓋,
# 請把它放在HOME/.matplotlib/matplotlibrc(unix/linux)
# C:\Documents and Settings\yourname\.matplotlib (win32 systems).
#
# 這個文件最好在支持python語法高亮模式的文本編輯器下查看。
# 空行或者註釋符開頭的行或者尾部追加的註釋都會被忽略。其它行都遵循下面格式:
# 關鍵字 : 值 # 可選註釋
#
# 顏色:下面是顏色值,你可使用一個matplotlib顏色字符串如:r, k或者b等,
# 也可使用rgb元組,如:(1.0, 0.5, 0.0),
# 也可使用16進制字符串,如ff00ff或者#ff00ff。
# 也可使用灰度值,如:0.75
# 還可使用合法的html顏色值:如: red, blue, darkslategrayshell

#### 開始配置

# 默認後端: 從GTK, GTKAgg, GTKCairo, CocoaAgg, FltkAgg, MacOSX, QtAgg, 
# Qt4Agg, TkAgg, WX, WXAgg, Agg, Cairo, GDK, PS, PDF, SVG, Template中選擇一個。
# 固然你也能夠本身在matplotlib外,經過'module://my_backend'引用模塊名(必須在python安裝目錄下),
# 來發佈一個後端。
backend : Agg

# 若是你使用Qt4Agg後端,你能夠選擇在這裏使用PyQt4或者新的PySide綁定到Qt4工具集。
#backend.qt4 : PyQt4 # PyQt4 | PySide

# 注意這個會重載被增強工具集(ETS)使用的QT_API環境變量;有效值爲"pyqt"和"pyside".
# "pyqt"設置會強制QString和QVariant使用版本2API的反作用

# 若是你在GUI裏使用pyplot和你選的後端衝突,
# 當你把backend_fallback設置爲True咱們會自動爲你選擇一個兼容後端。
#backend_fallback: True

#interactive : False
#toolbar : toolbar2 # None | classic | toolbar2
#timezone : UTC # a pytz timezone string, eg US/Central or Europe/Paris

# 若是你的matplotlib數據存放在非默認安裝的地方,像matplotlib字體,位圖等等
#datapath : /home/jdhunter/mpldata

### 線條
# 更多線條屬性詳情請查看
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大小定義
#font.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來檢測和刷新 
# 設置爲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 # 啓動全部的座標軸

# 控制下載示例數據。不少的示例能夠從matplotlib的git倉庫裏下載,剔除不少發行時的額外文件。
# 在這種狀況下,能夠將examples.download設置爲False以及
# 將examples.directory設置爲你想下載的地方: https://github.com/matplotlib/sample_data 

#examples.download : True # False會忽略下載機制
#examples.directory : '' # 若是download設置爲False時的查找目錄

相關文章
相關標籤/搜索