如何设置matplotlib自定义样式的路径?

Rez*_*ney 6 python matplotlib

根据 matplotlib 文档:

“您可以创建自定义样式并通过使用样式表的路径或 URL 调用 style.use 来使用它们。”

我试过:

plt.style.use('/usr/share/mygraph/mystyle.mplstyle')
Run Code Online (Sandbox Code Playgroud)

但它正在返回:

ValueError: '/usr/share/mygraph/mystyle.mplstyle' not found in the style library and input is not a valid URL or path. See `style.available` for list of available styles.
Run Code Online (Sandbox Code Playgroud)

有人可以帮助我如何正确设置路径吗?

有没有其他方法可以实现这一目标?

我想在 rpm 包中分发我的自定义样式,因此将它放在用户的主目录中会很棘手。

设置“MPLCONFIGDIR”环境变量也可以,但我想避免这种情况。

谢谢你的帮助!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~ 编辑:

由于我的冲动,我忘记将文件复制到那里。关于无效 URL 的输出错误让我感到困惑,所以我认为整个路径无效并专注于此。耻辱我!

Vin*_*ooo 0

也许对于未来的读者来说,知道还可以更改样式文件夹(您可以使用 获取当前文件夹matplotlib.get_configdir())会很有用。根据文件

您还可以通过设置MPLCONFIGDIR环境变量来更改 Matplotlib 查找 stylelib/ 文件夹的目录,请参阅 matplotlib 配置和缓存目录位置