You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

安装opencv-python后Jupyter中matplotlib'science'样式报错求助

问题:Matplotlib 'science' 样式无法加载

问题场景

在Python 3.10环境的Jupyter Notebook中运行以下代码:

import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
plt.style.use(['science', 'notebook'])
import sympy as smp
from skimage import color
from skimage import io
from scipy.fft import fftfreq
from scipy.fft import fft, ifft, fft2, ifft2

此前代码可正常运行,但执行%pip install opencv-python后,出现如下错误:

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
File ~\anaconda3\lib\site-packages\matplotlib\style\core.py:166, in use(style)
    165 try:
---> 166     style = _rc_params_in_file(style)
    167 except IOError as err:

File ~\anaconda3\lib\site-packages\matplotlib\__init__.py:850, in _rc_params_in_file(fname, transform, fail_on_error)
    849 rc_temp = {}
---> 850 with _open_file_or_url(fname) as fd:
    851     try:

File ~\anaconda3\lib\contextlib.py:135, in _GeneratorContextManager.__enter__(self)
    134 try:
---> 135     return next(self.gen)
    136 except StopIteration:

File ~\anaconda3\lib\site-packages\matplotlib\__init__.py:827, in _open_file_or_url(fname)
    826 fname = os.path.expanduser(fname)
---> 827 with open(fname, encoding='utf-8') as f:
    828     yield f

FileNotFoundError: [Errno 2] No such file or directory: 'science'

The above exception was the direct cause of the following exception:

OSError                                   Traceback (most recent call last)
Cell In[1], line 4
      2 import scipy as sp
      3 import matplotlib.pyplot as plt
----> 4 plt.style.use(['science', 'notebook'])
      5 import sympy as smp
      6 from skimage import color

File ~\anaconda3\lib\site-packages\matplotlib\style\core.py:168, in use(style)
    166         style = _rc_params_in_file(style)
    167     except IOError as err:
---> 168         raise IOError(
    169             f"{style!r} is not a valid package style, path of style "
    170             f"file, URL of style file, or library style name (library "
    171             f"styles are listed in `style.available`)") from err
    172 filtered = {}
    173 for k in style:  # don't trigger RcParams.__getitem__('backend')

OSError: 'science' is not a valid package style, path of style file, URL of style file, or library style name (library styles are listed in `style.available`)

解决方法

  • 检查science样式包状态:在Jupyter单元格执行%pip list | findstr matplotlib-science(Windows)或%pip list | grep matplotlib-science(Linux/macOS),确认包是否存在。若不存在,说明安装opencv时可能破坏了依赖
  • 重新安装样式包:执行%pip install matplotlib-science
  • 验证安装结果:运行plt.style.available,查看输出列表中是否包含science和notebook
  • 若问题仍存在,尝试更新matplotlib:%pip install --upgrade matplotlib

内容的提问来源于stack exchange,提问作者user22058539

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 00:05:28