當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python matplotlib rc_context用法及代碼示例

本文簡要介紹 python 語言中 matplotlib.pyplot.rc_context 的用法。

用法

matplotlib.pyplot.rc_context(rc=None, fname=None)

返回用於臨時更改 rcParams 的上下文管理器。

rcParams["backend"] 不會被上下文管理器重置。

通過上下文管理器調用和上下文主體中的 rcParams 更改將在上下文退出時重置。

參數
rc dict

要臨時設置的 rcParams。

fname str 或 path-like

具有 Matplotlib rc 設置的文件。如果同時給出fnamerc,則來自rc 的設置優先。

例子

通過 dict 傳遞顯式值:

with mpl.rc_context({'interactive': False}):
    fig, ax = plt.subplots()
    ax.plot(range(3), range(3))
    fig.savefig('example.png')
    plt.close(fig)

從文件加載設置:

with mpl.rc_context(fname='print.rc'):
    plt.plot(x, y)  # uses 'print.rc'

在上下文主體中設置:

with mpl.rc_context():
    # will be reset
    mpl.rcParams['lines.linewidth'] = 5
    plt.plot(x, y)

相關用法


注:本文由純淨天空篩選整理自skytowner.com大神的英文原創作品 matplotlib.pyplot.rc_context。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。