【问题标题】:How to permanently set matplotlib pyplot style?如何永久设置 matplotlib pyplot 样式?
【发布时间】:2017-08-08 12:00:19
【问题描述】:

我喜欢 matplotlib 中可用的 ggplot 样式。因此,当我在交互式会话中时,我通常会这样做

import matplotlib.pyplot as plt
plt.style.use('ggplot')

这会产生非常好的样式。有没有一种简单的方法可以使这个设置持久化,这样我每次启动 Python 时都不需要输入上述命令?

【问题讨论】:

  • 编辑 matplotlibrc 文件:matplotlib.org/users/customizing.html
  • 我在那里看不到“风格”的设置。如果您这样做,我认为您需要手动指定所有单独的设置。
  • 除了此处讨论的选项外,可能还有其他选项,具体取决于您所说的“我在交互式会话中”的含义。 IE。一些 IDE 允许定义一个类似于 import matplotlib; matplotlib.style.use("ggplot") 的启动命令。
  • 这是一个有趣的想法@ImportanceOfBeingErnest

标签: python matplotlib


【解决方案1】:

你可以追加

use('ggplot')

.../lib/python2.7/site-packages/matplotlib-2.0.0-py2.7-linux-x86_64.egg/matplotlib/style/__init__.py

您的具体路径可能略有不同。

【讨论】:

    【解决方案2】:

    您可以在安装目录中的matplotlibrc format file 中指定您需要的样式。

    编辑:我们在 github 上找到

    # from http://www.huyng.com/posts/sane-color-scheme-for-matplotlib/
    
    patch.linewidth: 0.5
    patch.facecolor: 348ABD  # blue
    patch.edgecolor: EEEEEE
    patch.antialiased: True
    
    font.size: 10.0
    
    axes.facecolor: E5E5E5
    axes.edgecolor: white
    axes.linewidth: 1
    axes.grid: True
    axes.titlesize: x-large
    axes.labelsize: large
    axes.labelcolor: 555555
    axes.axisbelow: True       # grid/ticks are below elements (e.g., lines, text)
    
    axes.prop_cycle: cycler('color', ['E24A33', '348ABD', '988ED5', '777777',     'FBC15E', '8EBA42', 'FFB5B8'])
                   # E24A33 : red
                   # 348ABD : blue
                   # 988ED5 : purple
                   # 777777 : gray
                   # FBC15E : yellow
                   # 8EBA42 : green
                   # FFB5B8 : pink
    
    xtick.color: 555555
    xtick.direction: out
    
    ytick.color: 555555
    ytick.direction: out
    
    grid.color: white
    grid.linestyle: -    # solid line
    
    figure.facecolor: white
    figure.edgecolor: 0.50
    

    【讨论】:

    【解决方案3】:

    原则上,@Roelants 的答案是正确的。在这里更详细一点:

    1. 按照here 的讨论找到您的matplotlibrc 文件并进行备份。
    2. 找到ggplot.mplstyle(与matplotlibrc 文件相比,可能位于子文件夹中)。
    3. matplotlibrc文件的内容替换为ggplot.mplstyle的内容。

    从那一刻起,默认样式将与 ggplot 样式定义的样式相同。

    【讨论】:

      【解决方案4】:

      我希望更详细地扩展@ImportanceOfBeingErnest 的答案。

      如何永久更改默认 Matplotlib 样式

      这是一个三步过程,我们 1. 找出 matplotlibrc 文件所在的位置 2. 将该文件复制到另一个位置 3. 添加特定样式的 matplotlibrc 参数。让我们更详细地了解每一个。

      1. 使用命令查找默认matplotlibrc 文件在您的计算机中的位置
      import matplotlib
      print(matplotlib.matplotlib_fname())
      
      1. 使用命令将默认matplotlibrc文件复制到目录 $HOME/.config/matplotlib/matplotlibrc
      cp path_matplotlibrc $HOME/.config/matplotlib/matplotlibrc`
      

      其中path_matplotlibrc是matplotlibrc所在的路径,即path_matplotlibrc是第一步的结果。

      1. 从其中一个样式文件中复制代码并将其粘贴到“$HOME/.config/matplotlib/matplotlibrc”文件中。

      转到目录matplotlib/lib/matplotlib/mpl-data/stylelib(或直接转到matplotlib 的github here 中的同一位置)。从该目录中的.mplstyle 文件之一复制代码。此代码将是matplotlibrc 参数列表,这些参数对应于文件名指示的特定样式。例如,去github,你可以直接复制fivethirtyeight样式的代码,如下所示。

      将带有该样式的matplotlibrc 参数的代码粘贴到新文件末尾的“$HOME/.config/matplotlib/matplotlibrcfile. So in this example we would paste the thefivethirtyeightstylematplotlibrc”参数代码的底部。

      这样就完成了。只需找出matplotlibrc 文件最初所在的位置,将该文件复制到另一个位置,然后添加特定样式的matplotlibrc 参数即可。很简单。

      为什么会这样?

      matplotlib 文档 here 解释说:

      Matplotlib 使用 matplotlibrc 配置文件来自定义各种属性,我们称之为“rc 设置”或“rc 参数”。您可以控制 Matplotlib 中几乎所有属性的默认值:图形大小和 DPI、线宽、颜色和样式、轴、轴和网格属性、文本和字体属性等等。在启动时读取 matplotlibrc 以配置 Matplotlib...当使用 style.use('/.mplstyle') 给出样式表时,样式表中指定的设置优先于 matplotlibrc 文件中的设置。

      每种样式实际上都是参数和属性的集合,它们覆盖了默认的matplotlibrc 文件here。我们可以通过查看目录 matplotlib/lib/matplotlib/mpl-data/stylelib(在 github here 上找到)明确地看到这一点,其中包含每个内置样式的文件。点击目录中的一个文件,我们可以清楚地看到样式只是matplotlibrc参数的列表。 (这就是我们上面展示的,fivethirtyeightmatplotlibrc 参数。)

      所以Matplotlib 使用matplotlibrc 文件来确定图形的样式。现在将某种样式的matplotlibrc 设置粘贴到新文件位置如何覆盖默认的matplotlibrc 设置?

      Matplotlib 按以下顺序在四个位置查找matplotlibrc 文件:

      1. matplotlibrc 在当前工作目录中。它只是查看当前工作目录中是否存在名为 matplotlibrc 的文件。
      2. $MATPLOTLIBRC 如果是文件,则为 $MATPLOTLIBRC/matplotlibrc
      3. 在 Linux 和 FreeBSD 上,如果您已自定义环境,它会在 .config/matplotlib/matplotlibrc(或 $XDG_CONFIG_HOME/matplotlib/matplotlibrc)中查找。在其他平台上,它会在 .matplotlib/matplotlibrc 中查找。
      4. INSTALL/matplotlib/mpl-data/matplotlibrc,其中INSTALL 是您安装matplotlib 软件包的位置。在 Linux 上可能类似于 /usr/lib/python3.7/site-packages,在 Windows 上可能类似于 C:\Python37\Lib\site-packages

      一旦找到matplotlibrc 文件,它就不会搜索任何其他路径。

      所以默认情况下matplotlibrc只能在位置四找到。这实际上意味着我们可以通过在位置 1、2 或 3 中创建新的matplotlibrc 来覆盖位置 4 中的默认设置。按照惯例,您应该在可能的最高编号位置创建一个新的matplotlibrc 文件,以便在需要时有更多空间来覆盖该文件。在我刚刚给出的说明中,我们在位置 3 中创建了一个新的 matplotlibrc 文件。这就是我们为什么以及如何覆盖默认的 matplotlibrc 设置,从而创建与现在使用的特定样式相对应的新 matplotlibrc 设置默认情况下。


      更多关于Matplotlib样式的信息可以在这里找到。

      关于Matplotlib样式

      matplotlib 有多种内置样式可供选择。您可以看到here 每种内置样式将如何改变您的绘图外观。要调用特定样式,请使用命令plt.style.use('stylename'),其中stylename 是任意样式名称,要列出所有可用样式,请使用print(plt.style.available)

      【讨论】:

        猜你喜欢
        • 2015-07-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-29
        相关资源
        最近更新 更多