设置和获取

pylot接口允许您使用setp和getp来设置和获取对象属性,以及对象进行内省。

设置

要将线条的线型设置为虚线,您可以执行以下操作:

  1. >>> line, = plt.plot([1,2,3])
  2. >>> plt.setp(line, linestyle='--')

如果要了解有效的参数类型,可以提供要设置的属性的名称而不使用值:

  1. >>> plt.setp(line, 'linestyle')
  2. linestyle: [ '-' | '--' | '-.' | ':' | 'steps' | 'None' ]

如果要查看可以设置的所有属性及其可能的值,您可以执行以下操作:

  1. >>> plt.setp(line)

set在单个实例或实例列表上运行。如果您处于查询模式内省可能的值,则仅使用序列中的第一个实例。实际设置值时,全部 实例将被设置。 例如,假设您有两行的列表,以下将使两行变粗和变红:

  1. >>> x = np.arange(0,1.0,0.01)
  2. >>> y1 = np.sin(2*np.pi*x)
  3. >>> y2 = np.sin(4*np.pi*x)
  4. >>> lines = plt.plot(x, y1, x, y2)
  5. >>> plt.setp(lines, linewidth=2, color='r')

获取

get返回给定属性的值。 您可以使用get来查询单个属性的值:

  1. >>> plt.getp(line, 'linewidth')
  2. 0.5

或所有属性/值对:

  1. >>> plt.getp(line)
  2. aa = True
  3. alpha = 1.0
  4. antialiased = True
  5. c = b
  6. clip_on = True
  7. color = b
  8. ... long listing skipped ...

别名

为了减少交互模式中的击键,许多属性具有短的别名,例如,’linew’为’lw’,’markeredgecolor’为’mec’。 当调用set或get in introspection模式时,这些属性将被列为’fullname或aliasname’。

别名

输出:

  1. Line setters
  2. agg_filter: a filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) array
  3. alpha: float
  4. animated: bool
  5. antialiased: bool
  6. clip_box: `.Bbox`
  7. clip_on: bool
  8. clip_path: [(`~matplotlib.path.Path`, `.Transform`) | `.Patch` | None]
  9. color: color
  10. contains: callable
  11. dash_capstyle: {'butt', 'round', 'projecting'}
  12. dash_joinstyle: {'miter', 'round', 'bevel'}
  13. dashes: sequence of floats (on/off ink in points) or (None, None)
  14. drawstyle: {'default', 'steps', 'steps-pre', 'steps-mid', 'steps-post'}
  15. figure: `.Figure`
  16. fillstyle: {'full', 'left', 'right', 'bottom', 'top', 'none'}
  17. gid: str
  18. in_layout: bool
  19. label: object
  20. linestyle: {'-', '--', '-.', ':', '', (offset, on-off-seq), ...}
  21. linewidth: float
  22. marker: unknown
  23. markeredgecolor: color
  24. markeredgewidth: float
  25. markerfacecolor: color
  26. markerfacecoloralt: color
  27. markersize: float
  28. markevery: unknown
  29. path_effects: `.AbstractPathEffect`
  30. picker: float or callable[[Artist, Event], Tuple[bool, dict]]
  31. pickradius: float
  32. rasterized: bool or None
  33. sketch_params: (scale: float, length: float, randomness: float)
  34. snap: bool or None
  35. solid_capstyle: {'butt', 'round', 'projecting'}
  36. solid_joinstyle: {'miter', 'round', 'bevel'}
  37. transform: matplotlib.transforms.Transform
  38. url: str
  39. visible: bool
  40. xdata: 1D array
  41. ydata: 1D array
  42. zorder: float
  43. Line getters
  44. agg_filter = None
  45. alpha = None
  46. animated = False
  47. antialiased = True
  48. children = []
  49. clip_box = TransformedBbox( Bbox(x0=0.0, y0=0.0, x1=1.0, ...
  50. clip_on = True
  51. clip_path = None
  52. color = r
  53. contains = None
  54. dash_capstyle = butt
  55. dash_joinstyle = round
  56. data = (array([0. , 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, ...
  57. drawstyle = default
  58. figure = Figure(640x480)
  59. fillstyle = full
  60. gid = None
  61. in_layout = True
  62. label = _line0
  63. linestyle = --
  64. linewidth = 2.0
  65. marker = None
  66. markeredgecolor = r
  67. markeredgewidth = 1.0
  68. markerfacecolor = r
  69. markerfacecoloralt = none
  70. markersize = 6.0
  71. markevery = None
  72. path = Path(array([[ 0.00000000e+00, 0.00000000e+00], ...
  73. path_effects = []
  74. picker = None
  75. pickradius = 5
  76. rasterized = None
  77. sketch_params = None
  78. snap = None
  79. solid_capstyle = projecting
  80. solid_joinstyle = round
  81. transform = CompositeGenericTransform( TransformWrapper( ...
  82. transformed_clip_path_and_affine = (None, None)
  83. url = None
  84. visible = True
  85. xdata = [0. 0.01 0.02 0.03 0.04 0.05]...
  86. xydata = [[0. 0. ] [0.01 0.06279052] ...
  87. ydata = [0. 0.06279052 0.12533323 0.18738131 0.248...
  88. zorder = 2
  89. Rectangle setters
  90. agg_filter: a filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) array
  91. alpha: float or None
  92. animated: bool
  93. antialiased: unknown
  94. capstyle: {'butt', 'round', 'projecting'}
  95. clip_box: `.Bbox`
  96. clip_on: bool
  97. clip_path: [(`~matplotlib.path.Path`, `.Transform`) | `.Patch` | None]
  98. color: color
  99. contains: callable
  100. edgecolor: color or None or 'auto'
  101. facecolor: color or None
  102. figure: `.Figure`
  103. fill: bool
  104. gid: str
  105. hatch: {'/', '\\', '|', '-', '+', 'x', 'o', 'O', '.', '*'}
  106. height: unknown
  107. in_layout: bool
  108. joinstyle: {'miter', 'round', 'bevel'}
  109. label: object
  110. linestyle: {'-', '--', '-.', ':', '', (offset, on-off-seq), ...}
  111. linewidth: float or None for default
  112. path_effects: `.AbstractPathEffect`
  113. picker: None or bool or float or callable
  114. rasterized: bool or None
  115. sketch_params: (scale: float, length: float, randomness: float)
  116. snap: bool or None
  117. transform: `.Transform`
  118. url: str
  119. visible: bool
  120. width: unknown
  121. x: unknown
  122. xy: (float, float)
  123. y: unknown
  124. zorder: float
  125. Rectangle getters
  126. agg_filter = None
  127. alpha = None
  128. animated = False
  129. antialiased = True
  130. bbox = Bbox(x0=0.0, y0=0.0, x1=1.0, y1=1.0)
  131. capstyle = butt
  132. children = []
  133. clip_box = None
  134. clip_on = True
  135. clip_path = None
  136. contains = None
  137. data_transform = BboxTransformTo( TransformedBbox( Bbox...
  138. edgecolor = (0.0, 0.0, 0.0, 0.0)
  139. extents = Bbox(x0=80.0, y0=52.8, x1=576.0, y1=422.4)
  140. facecolor = (1.0, 1.0, 1.0, 1.0)
  141. figure = Figure(640x480)
  142. fill = True
  143. gid = None
  144. hatch = None
  145. height = 1.0
  146. in_layout = True
  147. joinstyle = miter
  148. label =
  149. linestyle = solid
  150. linewidth = 0.0
  151. patch_transform = CompositeGenericTransform( BboxTransformTo( ...
  152. path = Path(array([[0., 0.], [1., 0.], [1.,...
  153. path_effects = []
  154. picker = None
  155. rasterized = None
  156. sketch_params = None
  157. snap = None
  158. transform = CompositeGenericTransform( CompositeGenericTra...
  159. transformed_clip_path_and_affine = (None, None)
  160. url = None
  161. verts = [[ 80. 52.8] [576. 52.8] [576. 422.4] [ 80...
  162. visible = True
  163. width = 1.0
  164. window_extent = Bbox(x0=80.0, y0=52.8, x1=576.0, y1=422.4)
  165. x = 0.0
  166. xy = (0.0, 0.0)
  167. y = 0.0
  168. zorder = 1
  169. Text setters
  170. agg_filter: a filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) array
  171. alpha: float
  172. animated: bool
  173. backgroundcolor: color
  174. bbox: dict with properties for `.patches.FancyBboxPatch`
  175. clip_box: `matplotlib.transforms.Bbox`
  176. clip_on: bool
  177. clip_path: { (`.path.Path`, `.transforms.Transform`), `.patches.Patch`, None }
  178. color: color
  179. contains: callable
  180. figure: `.Figure`
  181. fontfamily: {FONTNAME, 'serif', 'sans-serif', 'cursive', 'fantasy', 'monospace'}
  182. fontname: {FONTNAME, 'serif', 'sans-serif', 'cursive', 'fantasy', 'monospace'}
  183. fontproperties: `.font_manager.FontProperties`
  184. fontsize: {size in points, 'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large'}
  185. fontstretch: {a numeric value in range 0-1000, 'ultra-condensed', 'extra-condensed', 'condensed', 'semi-condensed', 'normal', 'semi-expanded', 'expanded', 'extra-expanded', 'ultra-expanded'}
  186. fontstyle: {'normal', 'italic', 'oblique'}
  187. fontvariant: {'normal', 'small-caps'}
  188. fontweight: {a numeric value in range 0-1000, 'ultralight', 'light', 'normal', 'regular', 'book', 'medium', 'roman', 'semibold', 'demibold', 'demi', 'bold', 'heavy', 'extra bold', 'black'}
  189. gid: str
  190. horizontalalignment: {'center', 'right', 'left'}
  191. in_layout: bool
  192. label: object
  193. linespacing: float (multiple of font size)
  194. multialignment: {'left', 'right', 'center'}
  195. path_effects: `.AbstractPathEffect`
  196. picker: None or bool or float or callable
  197. position: (float, float)
  198. rasterized: bool or None
  199. rotation: {angle in degrees, 'vertical', 'horizontal'}
  200. rotation_mode: {None, 'default', 'anchor'}
  201. sketch_params: (scale: float, length: float, randomness: float)
  202. snap: bool or None
  203. text: string or object castable to string (but ``None`` becomes ``''``)
  204. transform: `.Transform`
  205. url: str
  206. usetex: bool or None
  207. verticalalignment: {'center', 'top', 'bottom', 'baseline', 'center_baseline'}
  208. visible: bool
  209. wrap: bool
  210. x: float
  211. y: float
  212. zorder: float
  213. Text getters
  214. agg_filter = None
  215. alpha = None
  216. animated = False
  217. bbox_patch = None
  218. children = []
  219. clip_box = None
  220. clip_on = True
  221. clip_path = None
  222. color = black
  223. contains = None
  224. figure = Figure(640x480)
  225. fontfamily = ['sans-serif']
  226. fontname = DejaVu Sans
  227. fontproperties = :family=sans-serif:style=normal:variant=normal:wei...
  228. fontsize = 12.0
  229. fontstyle = normal
  230. fontvariant = normal
  231. fontweight = normal
  232. gid = None
  233. horizontalalignment = center
  234. in_layout = True
  235. label =
  236. path_effects = []
  237. picker = None
  238. position = (0.5, 1.0)
  239. rasterized = None
  240. rotation = 0.0
  241. rotation_mode = None
  242. sketch_params = None
  243. snap = None
  244. stretch = normal
  245. text = Hi mom
  246. transform = CompositeGenericTransform( BboxTransformTo( ...
  247. transformed_clip_path_and_affine = (None, None)
  248. unitless_position = (0.5, 1.0)
  249. url = None
  250. usetex = False
  251. verticalalignment = baseline
  252. visible = True
  253. wrap = False
  254. zorder = 3
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. x = np.arange(0, 1.0, 0.01)
  4. y1 = np.sin(2*np.pi*x)
  5. y2 = np.sin(4*np.pi*x)
  6. lines = plt.plot(x, y1, x, y2)
  7. l1, l2 = lines
  8. plt.setp(lines, linestyle='--') # set both to dashed
  9. plt.setp(l1, linewidth=2, color='r') # line1 is thick and red
  10. plt.setp(l2, linewidth=1, color='g') # line2 is thinner and green
  11. print('Line setters')
  12. plt.setp(l1)
  13. print('Line getters')
  14. plt.getp(l1)
  15. print('Rectangle setters')
  16. plt.setp(plt.gca().patch)
  17. print('Rectangle getters')
  18. plt.getp(plt.gca().patch)
  19. t = plt.title('Hi mom')
  20. print('Text setters')
  21. plt.setp(t)
  22. print('Text getters')
  23. plt.getp(t)
  24. plt.show()

下载这个示例