1. 程式人生 > >matplotlib各個物件及子物件的理解

matplotlib各個物件及子物件的理解

前言:

matplotlib的程式設計類似於GUI的程式設計。

各個物件之間的關係和QT的GUI元件widgets之間的關係很相似,QWidgets就是一個QObject的子類—都有一個父類。一個沒有父類的視窗部件就會是頂級視窗部件,而一個有父類的視窗就會被包含到它的父類中。
QWidgets呼叫show()函式之後,將顯示這個視窗部件和它的子類部件,而plt.show()也類似這個原理,將figure和其子類的物件全部顯示出來。

figure


是最重要的,承載所有子物件,類似於QWidgets的頂級視窗部件。

axes(子區域,座標軸)


有了figure之後我們需要一個axes(子區域座標軸)來承載我們要畫的圖,比如:

  1. Axes3D():3D axes object.
  2. matplotlib.axes.Axes(fig,rect):2Daxes object。
    The Axes contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system.
    建立一個座標軸例項在圖figure中,其中rect引數用來確定在圖中的位置。左邊座標,底部座標,寬度,高度。例:rect=[0.15,0.1,0.7,0.5]。

繪出的圖


有了axes之後,我們就可以在上面畫圖了,繪出的影象,例如:
1. plot方法,返回一個line2D,lines物件。
2. plot_surface()方法,返回一個surface plot物件。
3. hist()方法,返回一個array,bins,patches

【list】。
4. bar()方法,返回一個container [ BarContainer ]。
5. scatter( ) ,返回一個PathCollection物件。

繪出的圖的屬性和方法

lines
在這裡插入圖片描述
container
class matplotlib.container.Container(kl, label=None)
Bases: tuple
Base class for containers.
Containers are classes that collect semantically related Artists such as the bars of a bar plot.

  • add_callback(func)
    Adds a callback function that will be called whenever one of the Artist’s properties changes.
    Returns an id that is useful for removing the callback with - remove_callback() later.
  • get_children()
  • get_label()
    Get the label used for this artist in the legend.
  • pchanged()
    Fire an event when property changed, calling all of the registered callbacks.
  • remove()
  • remove_callback(oid)
    Remove a callback based on its id.
    See also:
    add_callback() For adding callbacks
  • set_label(s)
    Set the label to s for auto legend.
    ACCEPTS: string or anything printable with ’%s’ conversion.
  • set_remove_method(f )

path
class matplotlib.path.Path(vertices, codes=None, _interpolation_steps=1, closed=False,readonly=False)
Bases: object
Path represents a series of possibly disconnected, possibly closed, line and curve segments.
少許方法例項

  • code_type
    alias of numpy.uint8
  • codes
    The list of codes in the Path as a 1-D numpy array. Each code is one of STOP, MOVETO,LINETO, CURVE3, CURVE4 or CLOSEPOLY. For codes that correspond to more than one vertex(CURVE3 and CURVE4), that code will be repeated so that the length of self.vertices andself.codes is always the same.
  • contains_path(path, transform=None)Returns whether this (closed) path completely contains the given path.
    If transform is not None, the path will be transformed before performing the test.
  • contains_point(point, transform=None, radius=0.0)
    Returns whether the (closed) path contains the given point.
    If transform is not None, the path will be transformed before performing the test.
    radius allows the path to be made slightly larger or smaller.
  • contains_points(points, transform=None, radius=0.0)
    Returns a bool array which is True if the (closed) path contains the corresponding point.
    If transform is not None, the path will be transformed before performing the test.radius allows the path to be made slightly larger or smaller.
  • copy()
    Returns a shallow copy of the Path, which will share the vertices and codes with the source Path.
  • deepcopy(memo=None)
    Returns a deepcopy of the Path. The Path will not be readonly, even if the source Path is.
  • get_extents(transform=None)
    Returns the extents (xmin, ymin, xmax, ymax) of the path.
    Unlike computing the extents on the vertices alone, this algorithm will take into account the
    curves and deal with control points appropriately.
  • has_nonfinite
    True if the vertices array has nonfinite values.
  • classmethod hatch(hatchpattern, density=6)
    Given a hatch specifier, hatchpattern, generates a Path that can be used in a repeated hatching pattern. density is the number of lines per unit square.
  • interpolated(steps)
    Returns a new path resampled to length N x steps. Does not currently handle interpolating curves.
  • intersects_bbox(bbox, filled=True)
    Returns True if this path intersects a given Bbox. filled, when True, treats the path as if it was filled. That is, if the path completely encloses the bounding box, intersects_bbox() will return True.

patches
以下的子類如下:
在這裡插入圖片描述

figure下子物件的用法


plt.colorbar(mappable= None,cax = None,ax = None,**kw)
Draw a colorbar in an existing axes.

  • 引數mappable是指colorbar應用到的影象或者ContourSet等等(繪出的圖)。
  • 引數ax是指繪圖所在的區域,也就是figure的子區域。