matplotlib FileNotFoundError,OSError

qacovj5a  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(188)

请帮助解决以下问题。
我试图在Juypter笔记本电脑的一个单元格中运行以下代码:

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

字符串
但收到以下错误消息:

---------------------------------------------------------------------------
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`)


我曾经运行代码,并期待没有错误信息。但它似乎是错误提示后,我运行以下代码在Juypter笔记本的一个单元格:

%pip install opencv-python


我使用的是Python 3.10。

ryevplcw

ryevplcw1#

情况似乎是plt.style.use(['science', 'notebook'])是一个错误的代码片段。
要调试此错误,请执行以下操作:

from matplotlib import style

print(plt.style.available)

字符串
这将返回以下内容:

[‘Solarize_Light2’, ‘_classic_test_patch’, ‘bmh’, ‘classic’, ‘dark_background’, ‘fast’, ‘fivethirtyeight’, ‘ggplot’,’grayscale’,’seaborn’,’seaborn-bright’,’seaborn-colorblind’, ‘seaborn-dark’, ‘seaborn-dark-palette’, ‘seaborn-darkgrid’, ‘seaborn-deep’, ‘seaborn-muted’, ‘seaborn-notebook’, ‘seaborn-paper’, ‘seaborn-pastel’, ‘seaborn-poster’,’seaborn-talk’,’seaborn-ticks’,’seaborn-white’,’seaborn-whitegrid’,’tableau-colorblind10′]


您可能正在尝试使用不在可用软件包列表中的软件包。请参见以下使用正确参考Example code used below source的示例:

import numpy as np
import matplotlib.pyplot as plt
  
from matplotlib import style
  
data = np.random.randn(50)
  
# consider this code which uses a package listed above.
plt.style.use('Solarize_Light2')
  
plt.plot(data)
  
plt.show()


希望能帮上忙

相关问题