pylab.show()沒有顯示圖形圖像(python的matplotlib畫圖包)


no display name and no $DISPLAY environment variable

============================

@Neil's answer is one (perfectly valid!) way of doing it, but you can also simply call matplotlib.use('Agg') before importing matplotlib.pyplot, and then continue as normal. 

E.g.

import matplotlib as mpl mpl.use('Agg') import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(111) ax.plot(range(10)) fig.savefig('temp.png')

You don't have to use the Agg backend, as well.  The pdf, ps, svg, agg, cairo, and gdk backends can all be used without an X-server.  However, only the Agg backend will be built by default (I think?), so there's a good chance that the other backends may not be enabled on your particular install.

Alternately, you can just set the backend parameter in your .matplotlibrc file to automatically have matplotlib.pyplot use the given renderer.

 

REF:

http://stackoverflow.com/questions/4931376/generating-matplotlib-graphs-without-a-running-x-server

http://stackoverflow.com/questions/2801882/generating-a-png-with-matplotlib-when-display-is-undefined

http://www.astrobetter.com/plotting-to-a-file-in-python/

 

============================

import pylab
pylab.ion()
x = pylab.arange( 0, 10, 0.1)
y = pylab.sin(x)
pylab.plot(x,y, 'ro-')
pylab.show() 
pylab.savefig('temp.png')

============================

  1. # plotting with the pylab module from matplotlib
  2. # free from: http://matplotlib.sourceforge.net/
  3. # used windows istaller matplotlib-0.90.0.win32-py2.5.exe
  4. # tested with Python25 EU 4/21/2007
  5. import math
  6. import pylab # matplotlib
  7. # create the x list data
  8. # arange() is just like range() but allows float numbers
  9. x_list = pylab.arange(0.0, 5.0, 0.01)
  10. # calculate the y list data
  11. y_list = []
  12. for x in x_list:
  13. y = math.cos(2*math.pi*x) * math.exp(-x)
  14. y_list.append(y)
  15. pylab.xlabel("x")
  16. pylab.ylabel("cos(2pi * x) * exp(-x)")
  17. # draw the plot with a blue line 'b' (is default)
  18. # using x,y data from the x_list and y_list
  19. # (these lists can be brought in from other programs)
  20. #
  21. # other drawing styles -->
  22. # 'r' red line, 'g' green line, 'y' yellow line
  23. # 'ro' red dots as markers, 'r.' smaller red dots, 'r+' red pluses
  24. # 'r--' red dashed line, 'g^' green triangles, 'bs' blue squares
  25. # 'rp' red pentagons, 'r1', 'r2', 'r3', 'r4' well, check out the markers
  26. #
  27. pylab.plot(x_list, y_list, 'b')
  28. # save the plot as a PNG image file (optional)
  29. pylab.savefig('Fig1.png')
  30. # show the pylab plot window
  31. # you can zoom the graph, drag the graph, change the margins, save the graph
  32. pylab.show()

==========================

matplotlib compiled fine, but nothing shows up when I use it

The first thing to try is a clean install and see if that helps.  If not, the best way to test your install is by running a script, rather than working interactively from a python shell or an integrated development environment such as IDLE which add additional complexities. Open up a UNIX shell or a DOS command prompt and cd into a directory containing a minimal example in a file. Something like simple_plot.py for example:

from pylab import * plot([1,2,3]) show() 

and run it with:

python simple_plot.py --verbose-helpful

This will give you additional information about which backends matplotlib is loading, version information, and more. At this point you might want to make sure you understand matplotlib’s configuration process, governed by the matplotlibrc configuration file which contains instructions within and the concept of the matplotlib backend.

=================

 

5 matplotlib-繪制精美的圖表

matplotlib 是python最著名的繪圖庫,它提供了一整套和matlab相似的命令API,十分適合交互式地進行制圖。而且也可以方便地將它作為繪圖控件,嵌入GUI應用程序中。

它的文檔相當完備,並且 Gallery頁面 中有上百幅縮略圖,打開之后都有源程序。因此如果你需要繪制某種類型的圖,只需要在這個頁面中瀏覽/復制/粘貼一下,基本上都能搞定。

本章節作為matplotlib的入門介紹,將較為深入地挖掘幾個例子,從中理解和學習matplotlib繪圖的一些基本概念。

5.1 快速繪圖

matplotlib的pyplot子庫提供了和matlab類似的繪圖API,方便用戶快速繪制2D圖表。讓我們先來看一個簡單的例子:

# -*- coding: utf-8 -*-
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 10, 1000) y = np.sin(x) z = np.cos(x**2) plt.figure(figsize=(8,4)) plt.plot(x,y,label="$sin(x)$",color="red",linewidth=2) plt.plot(x,z,"b--",label="$cos(x^2)$") plt.xlabel("Time(s)") plt.ylabel("Volt") plt.title("PyPlot First Example") plt.ylim(-1.2,1.2) plt.legend() plt.show() 
_images/pyplot_simple_plot.png

圖5.1 調用pyplot庫快速將數據繪制成曲線圖

matplotlib中的快速繪圖的函數庫可以通過如下語句載入:

import matplotlib.pyplot as plt 

pylab模塊

matplotlib還提供了名為pylab的模塊,其中包括了許多numpy和pyplot中常用的函數,方便用戶快速進行計算和繪圖,可以用於IPython中的快速交互式使用。

接下來調用figure創建一個繪圖對象,並且使它成為當前的繪圖對象。

plt.figure(figsize=(8,4)) 

也可以不創建繪圖對象直接調用接下來的plot函數直接繪圖,matplotlib會為我們自動創建一個繪圖對象。如果需要同時繪制多幅圖表的話,可以是給figure傳遞一個整數參數指定圖標的序號,如果所指定序號的繪圖對象已經存在的話,將不創建新的對象,而只是讓它成為當前繪圖對象。

通過figsize參數可以指定繪圖對象的寬度和高度,單位為英寸;dpi參數指定繪圖對象的分辨率,即每英寸多少個像素,缺省值為80。因此本例中所創建的圖表窗口的寬度為8*80 = 640像素。

但是用工具欄中的保存按鈕保存下來的png圖像的大小是800*400像素。這是因為保存圖表用的函數savefig使用不同的DPI配置,savefig函數也有一個dpi參數,如果不設置的話,將使用matplotlib配置文件中的配置,此配置可以通過如下語句進行查看,關於配置文件將在后面的章節進行介紹:

>>> import matplotlib >>> matplotlib.rcParams["savefig.dpi"] 100 

下面的兩行程序通過調用plot函數在當前的繪圖對象中進行繪圖:

plt.plot(x,y,label="$sin(x)$",color="red",linewidth=2) plt.plot(x,z,"b--",label="$cos(x^2)$") 

plot函數的調用方式很靈活,第一句將x,y數組傳遞給plot之后,用關鍵字參數指定各種屬性:

  • label : 給所繪制的曲線一個名字,此名字在圖示(legend)中顯示。只要在字符串前后添加"$"符號,matplotlib就會使用其內嵌的latex引擎繪制的數學公式。
  • color : 指定曲線的顏色
  • linewidth : 指定曲線的寬度

第二句直接通過第三個參數"b--"指定曲線的顏色和線型,這個參數稱為格式化參數,它能夠通過一些易記的符號快速指定曲線的樣式。其中b表示藍色,"--"表示線型為虛線。在IPython中輸入 "plt.plot?" 可以查看格式化字符串的詳細配置。

接下來通過一系列函數設置繪圖對象的各個屬性:

plt.xlabel("Time(s)") plt.ylabel("Volt") plt.title("PyPlot First Example") plt.ylim(-1.2,1.2) plt.legend() 
  • xlabel : 設置X軸的文字
  • ylabel : 設置Y軸的文字
  • title : 設置圖表的標題
  • ylim : 設置Y軸的范圍
  • legend : 顯示圖示

最后調用plt.show()顯示出我們創建的所有繪圖對象。

5.1.1 配置屬性

matplotlib所繪制的圖的每個組成部分都對應有一個對象,我們可以通過調用這些對象的屬性設置方法set_*或者pyplot的屬性設置函數setp設置其屬性值。例如plot函數返回一個 matplotlib.lines.Line2D 對象的列表,下面的例子顯示如何設置Line2D對象的屬性:

>>> import numpy as np >>> import matplotlib.pyplot as plt >>> x = np.arange(0, 5, 0.1) >>> line, = plt.plot(x, x*x) # plot返回一個列表,通過line,獲取其第一個元素 >>> # 調用Line2D對象的set_*方法設置屬性值 >>> line.set_antialiased(False) 
>>> # 同時繪制sin和cos兩條曲線,lines是一個有兩個Line2D對象的列表 >>> lines = plt.plot(x, np.sin(x), x, np.cos(x)) # >>> # 調用setp函數同時配置多個Line2D對象的多個屬性值 >>> plt.setp(lines, color="r", linewidth=2.0) 

這段例子中,通過調用Line2D對象line的set_antialiased方法,關閉對象的反鋸齒效果。或者通過調用plt.setp函數配置多個Line2D對象的顏色和線寬屬性。

同樣我們可以通過調用Line2D對象的get_*方法,或者plt.getp函數獲取對象的屬性值:

>>> line.get_linewidth() 1.0 >>> plt.getp(lines[0], "color") # 返回color屬性 'r' >>> plt.getp(lines[1]) # 輸出全部屬性 alpha = 1.0 animated = False antialiased or aa = True axes = Axes(0.125,0.1;0.775x0.8) ... ... 

注意getp函數只能對一個對象進行操作,它有兩種用法:

  • 指定屬性名:返回對象的指定屬性的值
  • 不指定屬性名:打印出對象的所有屬性和其值

matplotlib的整個圖表為一個Figure對象,此對象在調用plt.figure函數時返回,我們也可以通過plt.gcf函數獲取當前的繪圖對象:

>>> f = plt.gcf() >>> plt.getp(f) alpha = 1.0 animated = False ... 

Figure對象有一個axes屬性,其值為AxesSubplot對象的列表,每個AxesSubplot對象代表圖表中的一個子圖,前面所繪制的圖表只包含一個子圖,當前子圖也可以通過plt.gca獲得:

>>> plt.getp(f, "axes") [<matplotlib.axes.AxesSubplot object at 0x05CDD170>] >>> plt.gca() <matplotlib.axes.AxesSubplot object at 0x05CDD170> 

用plt.getp可以發現AxesSubplot對象有很多屬性,例如它的lines屬性為此子圖所包括的 Line2D 對象列表:

>>> alllines = plt.getp(plt.gca(), "lines") >>> alllines <a list of 3 Line2D objects> >>> alllines[0] == line # 其中的第一條曲線就是最開始繪制的那條曲線 True 

通過這種方法我們可以很容易地查看對象的屬性和它們之間的包含關系,找到需要配置的屬性。

5.2 繪制多軸圖

一個繪圖對象(figure)可以包含多個軸(axis),在Matplotlib中用軸表示一個繪圖區域,可以將其理解為子圖。上面的第一個例子中,繪圖對象只包括一個軸,因此只顯示了一個軸(子圖)。我們可以使用subplot函數快速繪制有多個軸的圖表。subplot函數的調用形式如下:

subplot(numRows, numCols, plotNum) 

subplot將整個繪圖區域等分為numRows行 * numCols列個子區域,然后按照從左到右,從上到下的順序對每個子區域進行編號,左上的子區域的編號為1。如果numRows,numCols和plotNum這三個數都小於10的話,可以把它們縮寫為一個整數,例如subplot(323)和subplot(3,2,3)是相同的。subplot在plotNum指定的區域中創建一個軸對象。如果新創建的軸和之前創建的軸重疊的話,之前的軸將被刪除。

下面的程序創建3行2列共6個軸,通過axisbg參數給每個軸設置不同的背景顏色。

for idx, color in enumerate("rgbyck"): plt.subplot(320+idx+1, axisbg=color) plt.show() 
_images/pyplot_subplot01.png

圖5.2 用subplot函數將Figure分為六個子圖區域

如果希望某個軸占據整個行或者列的話,可以如下調用subplot:

plt.subplot(221) # 第一行的左圖 plt.subplot(222) # 第一行的右圖 plt.subplot(212) # 第二整行 plt.show() 
_images/pyplot_subplot02.png

圖5.3 將Figure分為三個子圖區域

當繪圖對象中有多個軸的時候,可以通過工具欄中的Configure Subplots按鈕,交互式地調節軸之間的間距和軸與邊框之間的距離。如果希望在程序中調節的話,可以調用subplots_adjust函數,它有left, right, bottom, top, wspace, hspace等幾個關鍵字參數,這些參數的值都是0到1之間的小數,它們是以繪圖區域的寬高為1進行正規化之后的坐標或者長度。

5.3 配置文件

一幅圖有許多需要配置的屬性,例如顏色、字體、線型等等。我們在繪圖時,並沒有一一對這些屬性進行配置,許多都直接采用了Matplotlib的缺省配置。Matplotlib將缺省配置保存在一個文件中,通過更改這個文件,我們可以修改這些屬性的缺省值。

Matplotlib 使用配置文件 matplotlibrc 時的搜索順序如下:

  • 當前路徑 : 程序的當前路徑
  • 用戶配置路徑 : 通常為 HOME/.matplotlib/,可以通過環境變量MATPLOTLIBRC修改
  • 系統配置路徑 : 保存在 matplotlib的安裝目錄下的 mpl-data 下

通過下面的語句可以獲取用戶配置路徑:

>>> import matplotlib >>> matplotlib.get_configdir() 'C:\\Documents and Settings\\zhang\\.matplotlib' 

通過下面的語句可以獲得目前使用的配置文件的路徑:

>>> import matplotlib >>> matplotlib.matplotlib_fname() 'C:\\Python26\\lib\\site-packages\\matplotlib\\mpl-data\\matplotlibrc' 

由於在當前路徑和用戶配置路徑中都沒有找到位置文件,因此最后使用的是系統配置路徑下的配置文件。如果你將matplotlibrc復制一份到腳本的當前目錄下:

>>> import os >>> os.getcwd() 'C:\\zhang\\doc' 

復制配置文件之后再運行:

>>> matplotlib.matplotlib_fname() 'C:\\zhang\\doc\\matplotlibrc' 

如果你用文本編輯器打開此配置文件的話,你會發現它實際上是定義了一個字典。為了對眾多的配置進行區分,關鍵字可以用點分開。

配置文件的讀入可以使用 rc_params 函數,它返回一個配置字典:

>>> matplotlib.rc_params() {'agg.path.chunksize': 0,  'axes.axisbelow': False,  'axes.edgecolor': 'k',  'axes.facecolor': 'w',  ... ... 

在matplotlib模塊載入的時候會調用rc_params,並把得到的配置字典保存到rcParams變量中:

>>> matplotlib.rcParams {'agg.path.chunksize': 0, 'axes.axisbelow': False, ... ... 

matplotlib將使用rcParams中的配置進行繪圖。用戶可以直接修改此字典中的配置,所做的改變會反映到此后所繪制的圖中。例如下面的腳本所繪制的線將帶有圓形的點標識符:

>>> matplotlib.rcParams["lines.marker"] = "o" >>> import pylab >>> pylab.plot([1,2,3]) >>> pylab.show() 

為了方便配置,可以使用rc函數,下面的例子同時配置點標識符、線寬和顏色:

>>> matplotlib.rc("lines", marker="x", linewidth=2, color="red") 

如果希望恢復到缺省的配置(matplotlib載入時從配置文件讀入的配置)的話,可以調用 rcdefaults 函數。

>>> matplotlib.rcdefaults() 

如果手工修改了配置文件,希望重新從配置文件載入最新的配置的話,可以調用:

>>> matplotlib.rcParams.update( matplotlib.rc_params() ) 

5.4 Artist對象

matplotlib API包含有三層:

  • backend_bases.FigureCanvas : 圖表的繪制領域
  • backend_bases.Renderer : 知道如何在FigureCanvas上如何繪圖
  • artist.Artist : 知道如何使用Renderer在FigureCanvas上繪圖

FigureCanvas和Renderer需要處理底層的繪圖操作,例如使用wxPython在界面上繪圖,或者使用PostScript繪制PDF。Artist則處理所有的高層結構,例如處理圖表、文字和曲線等的繪制和布局。通常我們只和Artist打交道,而不需要關心底層的繪制細節。

Artists分為簡單類型和容器類型兩種。簡單類型的Artists為標准的繪圖元件,例如Line2D、 Rectangle、 Text、AxesImage 等等。而容器類型則可以包含許多簡單類型的Artists,使它們組織成一個整體,例如Axis、 Axes、Figure等。

直接使用Artists創建圖表的標准流程如下:

  • 創建Figure對象
  • 用Figure對象創建一個或者多個Axes或者Subplot對象
  • 調用Axies等對象的方法創建各種簡單類型的Artists

下面首先調用pyplot.figure輔助函數創建Figure對象,然后調用Figure對象的add_axes方法在其中創建一個Axes對象,add_axes的參數是一個形如[left, bottom, width, height]的列表,這些數值分別指定所創建的Axes對象相對於fig的位置和大小,取值范圍都在0到1之間:

>>> import matplotlib.pyplot as plt >>> fig = plt.figure() >>> ax = fig.add_axes([0.15, 0.1, 0.7, 0.3]) 

然后我們調用ax的plot方法繪圖,創建一條曲線,並且返回此曲線對象(Line2D)。

>>> line, = ax.plot([1,2,3],[1,2,1]) >>> ax.lines [<matplotlib.lines.Line2D object at 0x0637A3D0>] >>> line <matplotlib.lines.Line2D object at 0x0637A3D0> 

ax.lines是一個為包含ax的所有曲線的列表,后續的ax.plot調用會往此列表中添加新的曲線。如果想刪除某條曲線的話,直接從此列表中刪除即可。

Axes對象還包括許多其它的Artists對象,例如我們可以通過調用set_xlabel設置其X軸上的標題:

>>> ax.set_xlabel("time") 

如果我們查看set_xlabel的源代碼的話,會發現它是通過調用下面的語句實現的:

self.xaxis.set_label_text(xlabel) 

如果我們一直跟蹤下去,會發現Axes的xaxis屬性是一個XAxis對象:

>>> ax.xaxis <matplotlib.axis.XAxis object at 0x06343230> 

XAxis的label屬性是一個Text對象:

>>> ax.xaxis.label <matplotlib.text.Text object at 0x06343290> 

而Text對象的_text屬性為我們設置的值:

>>> ax.xaxis.label._text 'time' 

這些對象都是Artists,因此也可以調用它們的屬性獲取函數來獲得相應的屬性:

>>> ax.xaxis.label.get_text() 'time' 

5.4.1 Artist的屬性

圖表中的每個元素都用一個matplotlib的Artist對象表示,而每個Artist對象都有一大堆屬性控制其顯示效果。例如Figure對象和Axes對象都有patch屬性作為其背景,它的值是一個Rectangle對象。通過設置此它的一些屬性可以修改Figrue圖表的背景顏色或者透明度等屬性,下面的例子將圖表的背景顏色設置為綠色:

>>> fig = plt.figure() >>> fig.show() >>> fig.patch.set_color("g") >>> fig.canvas.draw() 

patch的color屬性通過set_color函數進行設置,屬性修改之后並不會立即反映到圖表的顯示上,還需要調用fig.canvas.draw()函數才能夠更新顯示。

下面是Artist對象都具有的一些屬性:

  • alpha : 透明度,值在0到1之間,0為完全透明,1為完全不透明
  • animated : 布爾值,在繪制動畫效果時使用
  • axes : 此Artist對象所在的Axes對象,可能為None
  • clip_box : 對象的裁剪框
  • clip_on : 是否裁剪
  • clip_path : 裁剪的路徑
  • contains : 判斷指定點是否在對象上的函數
  • figure : 所在的Figure對象,可能為None
  • label : 文本標簽
  • picker : 控制Artist對象選取
  • transform : 控制偏移旋轉
  • visible : 是否可見
  • zorder : 控制繪圖順序

Artist對象的所有屬性都通過相應的 get_* 和 set_* 函數進行讀寫,例如下面的語句將alpha屬性設置為當前值的一半:

>>> fig.set_alpha(0.5*fig.get_alpha()) 

如果你想用一條語句設置多個屬性的話,可以使用set函數:

>>> fig.set(alpha=0.5, zorder=2) 

使用前面介紹的 matplotlib.pyplot.getp 函數可以方便地輸出Artist對象的所有屬性名和值。

>>> plt.getp(fig.patch)  aa = True  alpha = 1.0  animated = False  antialiased or aa = True  ... ... 

5.4.2 Figure容器

現在我們知道如何觀察和修改已知的某個Artist對象的屬性,接下來要解決如何找到指定的Artist對象。前面我們介紹過Artist對象有容器類型和簡單類型兩種,這一節讓我們來詳細看看容器類型的內容。

最大的Artist容器是matplotlib.figure.Figure,它包括組成圖表的所有元素。圖表的背景是一個Rectangle對象,用Figure.patch屬性表示。當你通過調用add_subplot或者add_axes方法往圖表中添加軸(子圖時),這些子圖都將添加到Figure.axes屬性中,同時這兩個方法也返回添加進axes屬性的對象,注意返回值的類型有所不同,實際上AxesSubplot是Axes的子類。

>>> fig = plt.figure() >>> ax1 = fig.add_subplot(211) >>> ax2 = fig.add_axes([0.1, 0.1, 0.7, 0.3]) >>> ax1 <matplotlib.axes.AxesSubplot object at 0x056BCA90> >>> ax2 <matplotlib.axes.Axes object at 0x056BC910> >>> fig.axes [<matplotlib.axes.AxesSubplot object at 0x056BCA90>, <matplotlib.axes.Axes object at 0x056BC910>] 

為了支持pylab中的gca()等函數,Figure對象內部保存有當前軸的信息,因此不建議直接對Figure.axes屬性進行列表操作,而應該使用add_subplot, add_axes, delaxes等方法進行添加和刪除操作。但是使用for循環對axes中的每個元素進行操作是沒有問題的,下面的語句打開所有子圖的柵格。

>>> for ax in fig.axes: ax.grid(True) 

Figure對象可以擁有自己的文字、線條以及圖像等簡單類型的Artist。缺省的坐標系統為像素點,但是可以通過設置Artist對象的transform屬性修改坐標系的轉換方式。最常用的Figure對象的坐標系是以左下角為坐標原點(0,0),右上角為坐標(1,1)。下面的程序創建並添加兩條直線到fig中:

>>> from matplotlib.lines import Line2D >>> fig = plt.figure() >>> line1 = Line2D([0,1],[0,1], transform=fig.transFigure, figure=fig, color="r") >>> line2 = Line2D([0,1],[1,0], transform=fig.transFigure, figure=fig, color="g") >>> fig.lines.extend([line1, line2]) >>> fig.show() 
_images/pyplot_artist01.png

圖5.4 在Figure對象中手工繪制直線

注意為了讓所創建的Line2D對象使用fig的坐標,我們將fig.TransFigure賦給Line2D對象的transform屬性;為了讓Line2D對象知道它是在fig對象中,我們還設置其figure屬性為fig;最后還需要將創建的兩個Line2D對象添加到fig.lines屬性中去。

Figure對象有如下屬性包含其它的Artist對象:

  • axes : Axes對象列表
  • patch : 作為背景的Rectangle對象
  • images : FigureImage對象列表,用來顯示圖片
  • legends : Legend對象列表
  • lines : Line2D對象列表
  • patches : patch對象列表
  • texts : Text對象列表,用來顯示文字

5.4.3 Axes容器

Axes容器是整個matplotlib庫的核心,它包含了組成圖表的眾多Artist對象,並且有許多方法函數幫助我們創建、修改這些對象。和Figure一樣,它有一個patch屬性作為背景,當它是笛卡爾坐標時,patch屬性是一個Rectangle對象,而當它是極坐標時,patch屬性則是Circle對象。例如下面的語句設置Axes對象的背景顏色為綠色:

>>> fig = plt.figure() >>> ax = fig.add_subplot(111) >>> ax.patch.set_facecolor("green") 

當你調用Axes的繪圖方法(例如plot),它將創建一組Line2D對象,並將所有的關鍵字參數傳遞給這些Line2D對象,並將它們添加進Axes.lines屬性中,最后返回所創建的Line2D對象列表:

>>> x, y = np.random.rand(2, 100) >>> line, = ax.plot(x, y, "-", color="blue", linewidth=2) >>> line <matplotlib.lines.Line2D object at 0x03007030> >>> ax.lines [<matplotlib.lines.Line2D object at 0x03007030>] 

注意plot返回的是一個Line2D對象的列表,因為我們可以傳遞多組X,Y軸的數據,一次繪制多條曲線。

與plot方法類似,繪制直方圖的方法bar和繪制柱狀統計圖的方法hist將創建一個Patch對象的列表,每個元素實際上都是Patch的子類Rectangle,並且將所創建的Patch對象都添加進Axes.patches屬性中:

>>> ax = fig.add_subplot(111) >>> n, bins, rects = ax.hist(np.random.randn(1000), 50, facecolor="blue") >>> rects <a list of 50 Patch objects> >>> rects[0] <matplotlib.patches.Rectangle object at 0x05BC2350> >>> ax.patches[0] <matplotlib.patches.Rectangle object at 0x05BC2350> 

一般我們不會直接對Axes.lines或者Axes.patches屬性進行操作,而是調用add_line或者add_patch等方法,這些方法幫助我們完成許多屬性設置工作:

>>> fig = plt.figure() >>> ax = fig.add_subplot(111) >>> rect = matplotlib.patches.Rectangle((1,1), width=5, height=12) >>> print rect.get_axes() # rect的axes屬性為空 None >>> rect.get_transform() # rect的transform屬性為缺省值 BboxTransformTo(Bbox(array([[ 1., 1.],  [ 6., 13.]]))) >>> ax.add_patch(rect) # 將rect添加進ax <matplotlib.patches.Rectangle object at 0x05C34E50> >>> rect.get_axes() # 於是rect的axes屬性就是ax <matplotlib.axes.AxesSubplot object at 0x05C09CB0> 
>>> # rect的transform屬性和ax的transData相同 >>> rect.get_transform() ... # 太長,省略 >>> ax.transData ... # 太長,省略 
>>> ax.get_xlim() # ax的X軸范圍為0到1,無法顯示完整的rect (0.0, 1.0) >>> ax.dataLim._get_bounds() # 數據的范圍和rect的大小一致 (1.0, 1.0, 5.0, 12.0) >>> ax.autoscale_view() # 自動調整坐標軸范圍 >>> ax.get_xlim() # 於是X軸可以完整顯示rect (1.0, 6.0) >>> plt.show() 

通過上面的例子我們可以看出,add_patch方法幫助我們設置了rect的axes和transform屬性。

下面詳細列出Axes包含各種Artist對象的屬性:

  • artists : Artist對象列表
  • patch : 作為Axes背景的Patch對象,可以是Rectangle或者Circle
  • collections : Collection對象列表
  • images : AxesImage對象列表
  • legends : Legend對象列表
  • lines : Line2D對象列表
  • patches : Patch對象列表
  • texts : Text對象列表
  • xaxis : XAxis對象
  • yaxis : YAxis對象

下面列出Axes的創建Artist對象的方法:

Axes的方法 所創建的對象 添加進的列表
annotate Annotate texts
bars Rectangle patches
errorbar Line2D, Rectangle lines,patches
fill Polygon patches
hist Rectangle patches
imshow AxesImage images
legend Legend legends
plot Line2D lines
scatter PolygonCollection Collections
text Text texts

下面以繪制散列圖(scatter)為例,驗證一下:

>>> fig = plt.figure() >>> ax = fig.add_subplot(111) >>> t = ax.scatter(np.random.rand(20), np.random.rand(20)) >>> t # 返回值為CircleCollection對象 <matplotlib.collections.CircleCollection object at 0x06004230> >>> ax.collections # 返回的對象已經添加進了collections列表中 [<matplotlib.collections.CircleCollection object at 0x06004230>] >>> fig.show() >>> t.get_sizes() # 獲得Collection的點數 20 
_images/pyplot_artist02.png

圖5.5 用scatter函數繪制散列圖

5.4.4 Axis容器

Axis容器包括坐標軸上的刻度線、刻度文本、坐標網格以及坐標軸標題等內容。刻度包括主刻度和副刻度,分別通過Axis.get_major_ticks和Axis.get_minor_ticks方法獲得。每個刻度線都是一個XTick或者YTick對象,它包括實際的刻度線和刻度文本。為了方便訪問刻度線和文本,Axis對象提供了get_ticklabels和get_ticklines方法分別直接獲得刻度線和刻度文本:

>>> pl.plot([1,2,3],[4,5,6]) [<matplotlib.lines.Line2D object at 0x0AD3B670>] >>> pl.show() >>> axis = pl.gca().xaxis 
>>> axis.get_ticklocs() # 獲得刻度的位置列表 array([ 1. , 1.5, 2. , 2.5, 3. ]) 
>>> axis.get_ticklabels() # 獲得刻度標簽列表 <a list of 5 Text major ticklabel objects> >>> [x.get_text() for x in axis.get_ticklabels()] # 獲得刻度的文本字符串 [u'1.0', u'1.5', u'2.0', u'2.5', u'3.0'] 
>>> axis.get_ticklines() # 獲得主刻度線列表,圖的上下刻度線共10條 <a list of 10 Line2D ticklines objects> 
>>> axis.get_ticklines(minor=True) # 獲得副刻度線列表 <a list of 0 Line2D ticklines objects> 

獲得刻度線或者刻度標簽之后,可以設置其各種屬性,下面設置刻度線為綠色粗線,文本為紅色並且旋轉45度:

>>> for label in axis.get_ticklabels(): ... label.set_color("red") ... label.set_rotation(45) ... label.set_fontsize(16) ... 
>>> for line in axis.get_ticklines(): ... line.set_color("green") ... line.set_markersize(25) ... line.set_markeredgewidth(3) 

最終的結果圖如下:

_images/pyplot_axis01.png

圖5.6 手工配置X軸的刻度線和刻度文本的樣式

上面的例子中,獲得的副刻度線列表為空,這是因為用於計算副刻度的對象缺省為NullLocator,它不產生任何刻度線;而計算主刻度的對象為AutoLocator,它會根據當前的縮放等配置自動計算刻度的位置:

>>> axis.get_minor_locator() # 計算副刻度的對象 <matplotlib.ticker.NullLocator instance at 0x0A014300> >>> axis.get_major_locator() # 計算主刻度的對象 <matplotlib.ticker.AutoLocator instance at 0x09281B20> 

我們可以使用程序為Axis對象設置不同的Locator對象,用來手工設置刻度的位置;設置Formatter對象用來控制刻度文本的顯示。下面的程序設置X軸的主刻度為pi/4,副刻度為pi/20,並且主刻度上的文本以pi為單位:

# -*- coding: utf-8 -*-
import matplotlib.pyplot as pl from matplotlib.ticker import MultipleLocator, FuncFormatter import numpy as np x = np.arange(0, 4*np.pi, 0.01) y = np.sin(x) pl.figure(figsize=(8,4)) pl.plot(x, y) ax = pl.gca() def pi_formatter(x, pos): """  比較羅嗦地將數值轉換為以pi/4為單位的刻度文本  """ m = np.round(x / (np.pi/4)) n = 4 if m%2==0: m, n = m/2, n/2 if m%2==0: m, n = m/2, n/2 if m == 0: return "0" if m == 1 and n == 1: return "$\pi$" if n == 1: return r"$%d \pi$" % m if m == 1: return r"$\frac{\pi}{%d}$" % n return r"$\frac{%d \pi}{%d}$" % (m,n) # 設置兩個坐標軸的范圍 pl.ylim(-1.5,1.5) pl.xlim(0, np.max(x)) # 設置圖的底邊距 pl.subplots_adjust(bottom = 0.15) pl.grid() #開啟網格 # 主刻度為pi/4 ax.xaxis.set_major_locator( MultipleLocator(np.pi/4) ) # 主刻度文本用pi_formatter函數計算 ax.xaxis.set_major_formatter( FuncFormatter( pi_formatter ) ) # 副刻度為pi/20 ax.xaxis.set_minor_locator( MultipleLocator(np.pi/20) ) # 設置刻度文本的大小 for tick in ax.xaxis.get_major_ticks(): tick.label1.set_fontsize(16) pl.show() 

關於刻度的定位和文本格式的東西都在matplotlib.ticker中定義,程序中使用到如下兩個類:

  • MultipleLocator : 以指定值的整數倍為刻度放置刻度線
  • FuncFormatter : 使用指定的函數計算刻度文本,他會傳遞給所指定的函數兩個參數:刻度值和刻度序號,程序中通過比較笨的辦法計算出刻度值所對應的刻度文本

此外還有很多預定義的Locator和Formatter類,詳細內容請參考相應的API文檔。

_images/pyplot_axis02.png

圖5.7 手工配置X軸的刻度線的位置和文本,並開啟副刻度

=================

 REF:

http://matplotlib.org/faq/installing_faq.html#matplotlib-compiled-fine-but-nothing-shows-up-when-i-use-it

http://sebug.net/paper/books/scipydoc/matplotlib_intro.html

https://www.daniweb.com/software-development/python/code/216915/plotting-with-pylab

http://stackoverflow.com/questions/458209/is-there-a-way-to-detach-matplotlib-plots-so-that-the-computation-can-continue

http://stackoverflow.com/questions/9753885/pylab-matplotlib-show-waits-until-window-closes


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM