定制matplotlib
Created Saturday 08 December
2012
5.1
matplotlibrc文件
matplotlib使用matplotlibrc [matplotlib resource
configurations]配置文件來自定義各種屬性,我們稱之為rc配置或者rc參數。在matplotlib中你可以控制幾乎所有的默認屬性:視圖窗口大小以及每英寸點數[dpi],線條寬度,顏色和樣式,坐標軸,坐標和網格屬性,文本,字體等屬性。matplotlib從下面的3個地方按順序查找matplotlibrc文件:
1.
當前工作目錄下的matplotlibrc,通常用在你不想被其它地方使用的特定自定義[customizations]
2.
用戶默認的自定義在.matplotlib/matplotlibrc里。查看 .matplotlib目錄存放位置
3.
python安裝目錄/matplotlib/mpl-data/matplotlibrc.如果是linux,那么python安裝目錄像這樣:/usr/lib/python2.5/site-packages。如果在windows,可能是c:\pytho2.5\lib\site-packages。每次你安裝matplotlib,這個文件就會被覆蓋。如果你想要你的自定義配置保存下來,請把這個文件放到你的.matplotlib目錄下。
想查看當前工作的matplotlibrc文件是哪個,你可以使用下面的方式查看:
>>> import matplotlib
>>> matplotlib.matplotlib_fname()
'/home/foo/.matplotlib/matplotlibrc'
請看下面matplotlibrc文件例子
5.2 rc動態配置
你可以在python腳本或者python交互式環境里動態的改變默認rc配置。所有的rc配置變量稱為matplotlib.rcParams
使用字典格式存儲,它在matplotlib中是全局可見的。rcParams可以直接修改,如:
import matplotlib as mpl
mpl.rcParams['lines.linewidth'] = 2
mpl.rcParams['lines.color'] = 'r'
Matplotlib還提供了一些便利函數來修改rc配置。matplotlib.rc()命令利用關鍵字參數來一次性修改一個屬性的多個設置:
import matplotlib as mpl
mpl.rc('lines', linewidth=2, color='r')
這里matplotlib.rcdefaults()命令可以恢復為matplotlib標準默認配置。
還有可以驗證設置rcParams的值,詳情查看matplotlib.rcsetup。
5.2.1
一個matplotlibrc文件示例
### MATPLOTLIBRC 格式
#
這是一個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, darkslategray
#### 開始配置
# 默認后端: 從GTK, GTKAgg, GTKCairo, CocoaAgg, FltkAgg, MacOSX,
QtAgg,
# Qt4Agg, TkAgg, WX, WXAgg, Agg, Cairo, GDK, PS, PDF, SVG,
Template中選擇一個。
# 當然你也可以自己在matplotlib外,通過'
# 來發布一個后端。
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./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./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./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./api/artist_api.html#module-matplotlib.text
#text.color : black
### LaTex自定義
# 查看http://www./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./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./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./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./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時的查找目錄
==================================華麗的分割線========================================
中文問題,多么嚴重,有莫有呀,發個現成的:
用matplotlib作圖時默認設置下是無法顯示中文的
如何在matplotlib里顯示中文
|