【发布时间】:2020-12-16 18:43:58
【问题描述】:
尽管我在代码开头手动设置了每个绘图脚本中的绘图样式,但在导入模块后直接将 figure-plotting-style 设置为 seaborn-whitegrid,生成的数字包括没有网格显示的普通matplotlib.pyplot 标准白色背景,例如像这张图:
因此,我假设设置样式对我的脚本没有影响,但我不知道哪里出了问题,或者样式在哪里恢复为默认值。在任何给定点打印出当前活动的绘图样式是实用的,例如在调试会话期间。
这是我设置绘图样式的方式:
import .....
import matplotlib.pyplot as plt
import .....
# * Set the matplotlib.pyplot default template (plotting style and background) *
plt.style.use('seaborn-whitegrid')
# Start of script #
...
我什至在主脚本调用的绘图子模块中插入了 plt.style.use('seaborn-whitegrid') 行,但它似乎对输出没有影响。
编辑其他试验:
根据 @Bernardo Trindade 下面的建议,我将 plt.style.use 替换为 mpl.style.use:
# * Set the matplotlib.pyplot default template (plotting style and background) *
mpl_plt_default_template = 'seaborn-whitegrid'
import matplotlib as mpl
mpl.style.use(mpl_plt_default_template)
我也试过temporary styling,但也无济于事:
# Set plotting style
with plt.style.context('seaborn-whitegrid'):
# * Instantiate figure
fig, axs = plt.subplots(rows, cols, figsize=(width, height))
....
以上所有都没有导致输出图表有任何差异;仍然显示没有网格等的普通标准 matplotlib 背景。
系统细节:
Lubuntu 20.04 LTS, 蟒蛇3.9x, VS 代码
【问题讨论】:
标签: python python-3.x matplotlib plot seaborn