在 pandas
和 seaborn
中,可以通过使用 with
关键词临时更改显示/绘图选项,仅将指定的设置应用于缩进的代码,而保留全局设置不变:
print(pd.get_option("display.max_rows"))
with pd.option_context("display.max_rows",10):
print(pd.get_option("display.max_rows"))
print(pd.get_option("display.max_rows"))
输出:
60
10
60
当我尝试使用with mpl.rcdefaults():
或者with mpl.rc('lines', linewidth=2, color='r'):
时,我会收到AttributeError: __exit__
的错误信息。
是否有一种方法可以临时更改matplotlib中的rcParams,以便它们仅适用于所选择的代码子集,还是我必须手动来回切换?