如何设置默认的 matplotlib 样式?

How to set default matplotlib style?

使用matplotlib时,我倾向于使用

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

经常安静。有没有一种简单的方法可以将默认样式更改为 ggplot(或任何其他样式)?我查看了 'matplotlibrc' 文档,但找不到合适的选项。

有没有比 copying/linking 系统范围的 .mplstyle 更好的方法?

谢谢!

显然,还没有这样的选项。

但是,您可以告诉 iPython 在启动时加载 ggplot 样式,方法是将 "plt.style.use('ggplot')" 添加到 ipython_config.py 中的 c.InteractiveShellApp.exec_lines。

在以下位置创建一个文件(例如将其命名为 startup-01.py) ~/.ipython/profile_default/startup/

(根据需要用另一个配置文件名称替换 profile_default)

并在其中放置任何需要的笔记本(和交互式 ipython)初始化语句,包括

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

## other settings, e.g.
# plt.rcParams['figure.figsize'] = (10.0, 8.0)

您可以更改matplotlib 的设置文件。根据 docs :

matplotlib uses matplotlibrc configuration files to customize all kinds of properties, which we call rc settings or rc parameters. You can control the defaults of almost every property in matplotlib: figure size and dpi, line width, color and style, axes, axis and grid properties, text and font properties and so on.

您可以使用以下命令找到您的 matplotlibrc 文件:

import matplotlib
matplotlib.matplotlib_fname()

因此我们可以将 ggplot 设置放在 matplotlibrc 文件的末尾。您可以在官方 matplotlib 存储库中轻松找到 ggplot style (as well as other styles)。

plt.style.use('default') 对我有用。

据我了解,它告诉 matplotlib 切换回其 default 样式模式 .