【问题标题】:Axis grid is not displayed when employing explicitly grid-containing style with matplotlib.pyplot.style.use('seaborn-whitegrid')当使用 matplotlib.pyplot.style.use('seaborn-whitegrid') 明确使用包含网格的样式时,不显示轴网格
【发布时间】: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


    【解决方案1】:

    我认为没有存储当前使用的样式表名称的变量,因为所有样式信息都保存在一个大字典中,其值在调用matplotlib.style.use() 时会简单地更新。

    你试过了吗:

    import matplotlib as mpl
    mpl.style.use('seaborn-whitegrid')
    

    【讨论】:

    • 感谢您的意见。我已经在我所有的 python 脚本中替换了它,但它没有任何区别。如果您有更多想法,欢迎提出。
    • 您是否尝试过基于上下文的方法,例如这里提出的? matplotlib.org/3.3.3/tutorials/introductory/…
    • 我刚刚尝试过,但它对输出没有影响。我将把我的一个输出图表附加到我的原始帖子中。不知何故,总是使用 matplotlib - 默认绘图背景样式。我不明白为什么它不再起作用了。
    • 无论如何,感谢您的帮助。终于找到了解决办法,看我的回答:stackoverflow.com/a/65567436/12298276
    • 我很高兴你知道并感谢您发布答案。
    【解决方案2】:

    终于找到了罪魁祸首:

    ax.grid(which='both', alpha=1)
    

    以前,这条线从来没有引起过问题,网格显示得很好。 相比之下,现在这样设置网格会使网格不可见。


    1) 结论(简答)

    使其工作的最简单方法,即保护网格出现,是将参数b 设置为True,如下所示:

    ax.grid(b=True, which='both', alpha=1)
    

    或者,可以将 kwarg visible 设置为 True,如下所示:

    ax.grid(which='both', alpha=1, visible=True)
    

    我建议使用visible=True,因为与仅称为b 的可选参数相比,kwarg 的命名更直观地说明它的实际作用。


    2) 详细解答

    以下信息基于official matplotlib docs

    据称可选参数b必须手动设置为TrueFalse,否则在此上下文中它将设置为None, 即使当前的文档状态:

    b : bool 或 None,可选 是否显示网格线。如果提供了任何 kwargs,则假定您希望打开网格并且 b 将设置为 True。

    If b is None and there are no kwargs, this toggles the visibility of the lines.
    

    注意布尔参数“b”: 我尝试了TrueFalse,发现即使False 也提供了正确的网格,这是违反直觉的。

    相反,当显式放入None时,相当于什么都不传递,网格就不会出现:

    ax.grid(b=None, which='both', alpha=1)
    

    如前所述,网格在此上下文中也是不可见的,以下是我的原始代码行:

    ax.grid(which='both', alpha=1)
    

    浏览**kwargs 发现了一个额外的visibility kwarg,称为visible

    注意关于在其文档中命名 visibility kwarg

    Artist.set_visible(self, b)[来源]

    它被称为b,就像

    可选参数

    ax.grid().

    我确认他们确实在做同样的事情。

    当把 visible-kwarg 放到 True 时出现网格:

    ax.grid(b=None, which='both', alpha=1, visible=True)
    

    当设置矛盾的值时,会抛出以下错误:

    ValueError: 'b' and 'visible' specify inconsistent grid visibilities
    

    当采用以下两种可能性之一时会导致此错误:

    ax.grid(b=False, which='both', alpha=1, visible=True)
    ax.grid(b=True, which='both', alpha=1, visible=False)
    

    最终,要使网格成功显示,请将参数b 设置为True,如下所示:

    ax.grid(b=True, which='both', alpha=1)
    

    或者,可以将 kwarg visible 设置为 True,如下所示:

    ax.grid(which='both', alpha=1, visible=True)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多