定制对象
图表(figure)中的每个元素都由一个Matplotlib艺术家(Artist)表示,并且每个元素都有一个广泛的属性列表来配置其外观。图表(figure)本身包含一个与图表大小完全相同的矩形(Rectangle),可以使用该矩形设置图形的背景色和透明度。同样地每个Axes边界框(典型Matplotlib绘图中带有黑色边缘的标准白色框)都有一个矩形实例,用于确定Axes的颜色、透明度和其他属性。这些实例存储为成员变量Figure.patch和Axes.patch(“Patch”是从MATLAB继承的名称,是图形上的颜色2D“小块”,例如矩形、圆形和多边形)。每个Matplotlib艺术家(Artist)都具有以下属性:
Property 属性 | Description 描述 |
alpha | The transparency - a scalar from 0-1 透明度,取值0-1。 |
animated | A boolean that is used to facilitate animated drawing 是否动画加速,用于促进动画绘制的布尔值。 |
axes | The Axes that the Artist lives in, possibly None 艺术家(Artist)所在的图表域(Axes)容器,可能为空。 |
clip_box | The bounding box that clips the Artist 艺术家(Artist)的剪辑边界框。 |
clip_on | Whether clipping is enabled 是否启用剪辑。 |
clip_path | The path the artist is clipped to 艺术家(artist)剪辑到的路径。 |
contains | A picking function to test whether the artist contains the pick point 用于测试艺术家(Artist)是否包含拾取点的拾取函数。 |
figure | The figure instance the artist lives in, possibly None 艺术家(artist)所在的图表(figure),可能为空。 |
label | A text label (e.g., for auto-labeling) 文本标签(例如,用于自动标签)。 |
picker | A python object that controls object picking 控制对象拾取的python对象。 |
transform | The transformation 转换 |
visible | A boolean whether the artist should be drawn 是否应绘制艺术家(Artist)的布尔值。 |
zorder | A number which determines the drawing order 决定绘图顺序的数字。 |
rasterized | Boolean; Turns vectors into raster graphics (for compression & EPS transparency) 布尔值;将矢量转换为光栅图形(用于压缩和EPS透明度)。 |
每个属性都可以通过老式的setter或getter访问(是的,我们知道这会激怒Pythonistas,我们计划支持通过属性或特性直接访问,但目前还没有实现)。例如,要将当前透明度(alpha)乘以一半:
·
·
a = o.get_alpha()o.set_alpha(0.5*a)
完整代码:
·
·
·
·
·
·
·
·
·
·
·
·
·
·
ipython%matplotlib tkimport matplotlib.pyplot as pltfig = plt.figure()ax = fig.add_subplot(2, 1, 1) # 2行1列,第一个图表域(Axes)import numpy as npt = np.arange(0.0, 1.0, 0.01)s = np.sin(2*np.pi*t)line, = ax.plot(t, s, color='blue', lw=5)line2, = ax.plot(t, -s, color='red', lw=5)a = line2.get_alpha()if not a: a = 1line2.set_alpha(0.5*a)
改变透明度前后对比:
如果要同时设置多个属性,还可以使用带有关键字参数的set方法。例如:
·
o.set(alpha=0.5, zorder=2)
完整代码:
·
·
·
·
·
·
·
·
·
·
·
ipython%matplotlib tkimport matplotlib.pyplot as pltfig = plt.figure()ax = fig.add_subplot(2, 1, 1) # 2行1列,第一个图表域(Axes)import numpy as npt = np.arange(0.0, 1.0, 0.01)s = np.sin(2*np.pi*t)line, = ax.plot(t, s, color='blue', lw=5)line2, = ax.plot(t, -s, color='red', lw=5)line2.set(alpha=0.5, zorder=2)
如果以交互方式使用python shell,检查Artist属性的一种简便方法是使用matplotlib.artist.getp()函数(在pyplot中只需使用getp()),该函数列出属性及其值。这也适用于从Artist派生的类,例如Figure和Rectangle。以下是上面提到的图表(Figure)矩形属性:matplotlib.artist.getp():https://matplotlib.org/stable/api/_as_gen/matplotlib.artist.getp.html#matplotlib.artist.getpgetp():
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.getp.html#matplotlib.pyplot.getp
·
·
·
·
·
·
·
·
·
·
·
·
·
·
·
·
·
·
·
·
·
·
·
·
·
·
·
·
·
·
·
·
·
·
·
·
·
·
·
·
·
·
·
·
·
·
·
·
·
import matplotlibmatplotlib.artist.getp(fig.patch) agg_filter = None alpha = None angle = 0.0 animated = False antialiased or aa = False bbox = Bbox(x0=0.0, y0=0.0, x1=1.0, y1=1.0) capstyle = butt center = [0.5 0.5] children = [] clip_box = None clip_on = True clip_path = None corners = [[0. 0.] [1. 0.] [1. 1.] [0. 1.]] data_transform = BboxTransformTo( TransformedBbox( Bbox... edgecolor or ec = (1.0, 1.0, 1.0, 1.0) extents = Bbox(x0=0.0, y0=0.0, x1=640.0, y1=480.0) facecolor or fc = (1.0, 1.0, 1.0, 1.0) figure = Figure(640x480) fill = True gid = None hatch = None height = 1 in_layout = False joinstyle = miter label = linestyle or ls = solid linewidth or lw = 0.0 mouseover = False patch_transform = CompositeGenericTransform( BboxTransformTo( ... path = Path(array([[0., 0.], [1., 0.], [1.,... path_effects = [] picker = None rasterized = False sketch_params = None snap = None tightbbox = Bbox(x0=0.0, y0=0.0, x1=640.0, y1=480.0) transform = CompositeGenericTransform( CompositeGenericTra... transformed_clip_path_and_affine = (None, None) url = None verts = [[ 0. 0.] [640. 0.] [640. 480.] [ 0. 480.... visible = True width = 1 window_extent = Bbox(x0=0.0, y0=0.0, x1=640.0, y1=480.0) x = 0 xy = (0, 0) y = 0 zorder = 1
所有类的文档字符串中也包含Artist属性,因此您可以查阅交互式“帮助”或matplotlib.artist以获取给定对象的属性列表。matplotlib.artist:
https://matplotlib.org/stable/api/artist_api.html#artist-api