设置和获取
pylot接口允许您使用setp和getp来设置和获取对象属性,以及对象进行内省。
设置
要将线条的线型设置为虚线,您可以执行以下操作:
>>> line, = plt.plot([1,2,3])>>> plt.setp(line, linestyle='--')
如果要了解有效的参数类型,可以提供要设置的属性的名称而不使用值:
>>> plt.setp(line, 'linestyle')linestyle: [ '-' | '--' | '-.' | ':' | 'steps' | 'None' ]
如果要查看可以设置的所有属性及其可能的值,您可以执行以下操作:
>>> plt.setp(line)
set在单个实例或实例列表上运行。如果您处于查询模式内省可能的值,则仅使用序列中的第一个实例。实际设置值时,全部 实例将被设置。 例如,假设您有两行的列表,以下将使两行变粗和变红:
>>> x = np.arange(0,1.0,0.01)>>> y1 = np.sin(2*np.pi*x)>>> y2 = np.sin(4*np.pi*x)>>> lines = plt.plot(x, y1, x, y2)>>> plt.setp(lines, linewidth=2, color='r')
获取
get返回给定属性的值。 您可以使用get来查询单个属性的值:
>>> plt.getp(line, 'linewidth')0.5
或所有属性/值对:
>>> plt.getp(line)aa = Truealpha = 1.0antialiased = Truec = bclip_on = Truecolor = b... long listing skipped ...
别名
为了减少交互模式中的击键,许多属性具有短的别名,例如,’linew’为’lw’,’markeredgecolor’为’mec’。 当调用set或get in introspection模式时,这些属性将被列为’fullname或aliasname’。

输出:
Line settersagg_filter: a filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) arrayalpha: floatanimated: boolantialiased: boolclip_box: `.Bbox`clip_on: boolclip_path: [(`~matplotlib.path.Path`, `.Transform`) | `.Patch` | None]color: colorcontains: callabledash_capstyle: {'butt', 'round', 'projecting'}dash_joinstyle: {'miter', 'round', 'bevel'}dashes: sequence of floats (on/off ink in points) or (None, None)drawstyle: {'default', 'steps', 'steps-pre', 'steps-mid', 'steps-post'}figure: `.Figure`fillstyle: {'full', 'left', 'right', 'bottom', 'top', 'none'}gid: strin_layout: boollabel: objectlinestyle: {'-', '--', '-.', ':', '', (offset, on-off-seq), ...}linewidth: floatmarker: unknownmarkeredgecolor: colormarkeredgewidth: floatmarkerfacecolor: colormarkerfacecoloralt: colormarkersize: floatmarkevery: unknownpath_effects: `.AbstractPathEffect`picker: float or callable[[Artist, Event], Tuple[bool, dict]]pickradius: floatrasterized: bool or Nonesketch_params: (scale: float, length: float, randomness: float)snap: bool or Nonesolid_capstyle: {'butt', 'round', 'projecting'}solid_joinstyle: {'miter', 'round', 'bevel'}transform: matplotlib.transforms.Transformurl: strvisible: boolxdata: 1D arrayydata: 1D arrayzorder: floatLine gettersagg_filter = Nonealpha = Noneanimated = Falseantialiased = Truechildren = []clip_box = TransformedBbox( Bbox(x0=0.0, y0=0.0, x1=1.0, ...clip_on = Trueclip_path = Nonecolor = rcontains = Nonedash_capstyle = buttdash_joinstyle = rounddata = (array([0. , 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, ...drawstyle = defaultfigure = Figure(640x480)fillstyle = fullgid = Nonein_layout = Truelabel = _line0linestyle = --linewidth = 2.0marker = Nonemarkeredgecolor = rmarkeredgewidth = 1.0markerfacecolor = rmarkerfacecoloralt = nonemarkersize = 6.0markevery = Nonepath = Path(array([[ 0.00000000e+00, 0.00000000e+00], ...path_effects = []picker = Nonepickradius = 5rasterized = Nonesketch_params = Nonesnap = Nonesolid_capstyle = projectingsolid_joinstyle = roundtransform = CompositeGenericTransform( TransformWrapper( ...transformed_clip_path_and_affine = (None, None)url = Nonevisible = Truexdata = [0. 0.01 0.02 0.03 0.04 0.05]...xydata = [[0. 0. ] [0.01 0.06279052] ...ydata = [0. 0.06279052 0.12533323 0.18738131 0.248...zorder = 2Rectangle settersagg_filter: a filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) arrayalpha: float or Noneanimated: boolantialiased: unknowncapstyle: {'butt', 'round', 'projecting'}clip_box: `.Bbox`clip_on: boolclip_path: [(`~matplotlib.path.Path`, `.Transform`) | `.Patch` | None]color: colorcontains: callableedgecolor: color or None or 'auto'facecolor: color or Nonefigure: `.Figure`fill: boolgid: strhatch: {'/', '\\', '|', '-', '+', 'x', 'o', 'O', '.', '*'}height: unknownin_layout: booljoinstyle: {'miter', 'round', 'bevel'}label: objectlinestyle: {'-', '--', '-.', ':', '', (offset, on-off-seq), ...}linewidth: float or None for defaultpath_effects: `.AbstractPathEffect`picker: None or bool or float or callablerasterized: bool or Nonesketch_params: (scale: float, length: float, randomness: float)snap: bool or Nonetransform: `.Transform`url: strvisible: boolwidth: unknownx: unknownxy: (float, float)y: unknownzorder: floatRectangle gettersagg_filter = Nonealpha = Noneanimated = Falseantialiased = Truebbox = Bbox(x0=0.0, y0=0.0, x1=1.0, y1=1.0)capstyle = buttchildren = []clip_box = Noneclip_on = Trueclip_path = Nonecontains = Nonedata_transform = BboxTransformTo( TransformedBbox( Bbox...edgecolor = (0.0, 0.0, 0.0, 0.0)extents = Bbox(x0=80.0, y0=52.8, x1=576.0, y1=422.4)facecolor = (1.0, 1.0, 1.0, 1.0)figure = Figure(640x480)fill = Truegid = Nonehatch = Noneheight = 1.0in_layout = Truejoinstyle = miterlabel =linestyle = solidlinewidth = 0.0patch_transform = CompositeGenericTransform( BboxTransformTo( ...path = Path(array([[0., 0.], [1., 0.], [1.,...path_effects = []picker = Nonerasterized = Nonesketch_params = Nonesnap = Nonetransform = CompositeGenericTransform( CompositeGenericTra...transformed_clip_path_and_affine = (None, None)url = Noneverts = [[ 80. 52.8] [576. 52.8] [576. 422.4] [ 80...visible = Truewidth = 1.0window_extent = Bbox(x0=80.0, y0=52.8, x1=576.0, y1=422.4)x = 0.0xy = (0.0, 0.0)y = 0.0zorder = 1Text settersagg_filter: a filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) arrayalpha: floatanimated: boolbackgroundcolor: colorbbox: dict with properties for `.patches.FancyBboxPatch`clip_box: `matplotlib.transforms.Bbox`clip_on: boolclip_path: { (`.path.Path`, `.transforms.Transform`), `.patches.Patch`, None }color: colorcontains: callablefigure: `.Figure`fontfamily: {FONTNAME, 'serif', 'sans-serif', 'cursive', 'fantasy', 'monospace'}fontname: {FONTNAME, 'serif', 'sans-serif', 'cursive', 'fantasy', 'monospace'}fontproperties: `.font_manager.FontProperties`fontsize: {size in points, 'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large'}fontstretch: {a numeric value in range 0-1000, 'ultra-condensed', 'extra-condensed', 'condensed', 'semi-condensed', 'normal', 'semi-expanded', 'expanded', 'extra-expanded', 'ultra-expanded'}fontstyle: {'normal', 'italic', 'oblique'}fontvariant: {'normal', 'small-caps'}fontweight: {a numeric value in range 0-1000, 'ultralight', 'light', 'normal', 'regular', 'book', 'medium', 'roman', 'semibold', 'demibold', 'demi', 'bold', 'heavy', 'extra bold', 'black'}gid: strhorizontalalignment: {'center', 'right', 'left'}in_layout: boollabel: objectlinespacing: float (multiple of font size)multialignment: {'left', 'right', 'center'}path_effects: `.AbstractPathEffect`picker: None or bool or float or callableposition: (float, float)rasterized: bool or Nonerotation: {angle in degrees, 'vertical', 'horizontal'}rotation_mode: {None, 'default', 'anchor'}sketch_params: (scale: float, length: float, randomness: float)snap: bool or Nonetext: string or object castable to string (but ``None`` becomes ``''``)transform: `.Transform`url: strusetex: bool or Noneverticalalignment: {'center', 'top', 'bottom', 'baseline', 'center_baseline'}visible: boolwrap: boolx: floaty: floatzorder: floatText gettersagg_filter = Nonealpha = Noneanimated = Falsebbox_patch = Nonechildren = []clip_box = Noneclip_on = Trueclip_path = Nonecolor = blackcontains = Nonefigure = Figure(640x480)fontfamily = ['sans-serif']fontname = DejaVu Sansfontproperties = :family=sans-serif:style=normal:variant=normal:wei...fontsize = 12.0fontstyle = normalfontvariant = normalfontweight = normalgid = Nonehorizontalalignment = centerin_layout = Truelabel =path_effects = []picker = Noneposition = (0.5, 1.0)rasterized = Nonerotation = 0.0rotation_mode = Nonesketch_params = Nonesnap = Nonestretch = normaltext = Hi momtransform = CompositeGenericTransform( BboxTransformTo( ...transformed_clip_path_and_affine = (None, None)unitless_position = (0.5, 1.0)url = Noneusetex = Falseverticalalignment = baselinevisible = Truewrap = Falsezorder = 3
import matplotlib.pyplot as pltimport numpy as npx = np.arange(0, 1.0, 0.01)y1 = np.sin(2*np.pi*x)y2 = np.sin(4*np.pi*x)lines = plt.plot(x, y1, x, y2)l1, l2 = linesplt.setp(lines, linestyle='--') # set both to dashedplt.setp(l1, linewidth=2, color='r') # line1 is thick and redplt.setp(l2, linewidth=1, color='g') # line2 is thinner and greenprint('Line setters')plt.setp(l1)print('Line getters')plt.getp(l1)print('Rectangle setters')plt.setp(plt.gca().patch)print('Rectangle getters')plt.getp(plt.gca().patch)t = plt.title('Hi mom')print('Text setters')plt.setp(t)print('Text getters')plt.getp(t)plt.show()
