matplotlib.artist.setp#

matplotlib.artist.setp(obj, *args, file=None, **kwargs)[源码]#

Artist 上设置一个或多个属性,或列出允许的值。

参数:
objArtistArtist 列表

要设置或查询属性的 Artist 对象。设置属性时,所有 Artist 都受影响;查询允许值时,只查询序列中的第一个实例。

例如,可以使用一个调用将两条线的线宽变粗并变为红色

>>> x = arange(0, 1, 0.01)
>>> lines = plot(x, sin(2*pi*x), x, sin(4*pi*x))
>>> setp(lines, linewidth=2, color='r')
file文件类对象,默认值:sys.stdout

当请求列出允许值时,setp 写入输出的位置。

>>> with open('output.log') as file:
...     setp(line, file=file)

默认值 None 表示 sys.stdout

*args, **kwargs

要设置的属性。支持以下组合

  • 将线的线条样式设置为虚线

    >>> line, = plot([1, 2, 3])
    >>> setp(line, linestyle='--')
    
  • 一次设置多个属性

    >>> setp(line, linewidth=2, color='r')
    
  • 列出线的线条样式允许的值

    >>> setp(line, 'linestyle')
    linestyle: {'-', '--', '-.', ':', '', (offset, on-off-seq), ...}
    
  • 列出所有可设置的属性及其允许的值

    >>> setp(line)
    agg_filter: a filter function, ...
    [long output listing omitted]
    

setp 也支持 MATLAB 风格的字符串/值对。例如,以下是等效的

>>> setp(lines, 'linewidth', 2, 'color', 'r')  # MATLAB style
>>> setp(lines, linewidth=2, color='r')        # Python style

另请参阅

getp