如何修复 matplotlib OSError: xkcd not found in the style library

问题:

尝试使用以下命令启用 matplotlib xkcd 样式

xkcd_style_error.py
plt.style.use("xkcd")

你看到以下错误消息:

xkcd_error.txt
OSError: 'xkcd' not found in the style library and input is not a valid URL or path; see `style.available` for list of available styles

解决方案

你不能通过运行 plt.style.use("xkcd") 启用 xkcd 样式绘图。而是使用 with plt.xkcd()

xkcd_style_fix.py
with plt.xkcd():
    # TODO 你的绘图代码放在这里!
    # plt.plot(x, y) # Example

Check out similar posts by category: Python