Python报No such file or directory: ‘science‘的解决方法

接上一篇博文:时间序列异常检测论文TranAD: Deep Transformer Networks for Anomaly Detection in Multivariate Time Series Data-CSDN博客

还是想看看这篇论文的可视化结果。但是当我重新运行原版代码的时候,会报错:

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

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

Traceback (most recent call last):
  File "main.py", line 7, in 
    from src.plotting import *
  File "/home/quyu/Projects/TranAD-Original/src/plotting.py", line 7, in 
    plt.style.use(['science', 'ieee'])
  File "/home/quyu/anaconda3/envs/TranAD/lib/python3.8/site-packages/matplotlib/style/core.py", line 168, in use
    raise IOError(
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`)

搜了一下,发现并不是我一个人报这种错,例如:FileNotFoundError: [Errno 2] No such file or directory: 'science' · Issue #1 · MTandHJ/roboc · GitHub

按照这个issue的解释,应该装一下SciencePlots,我首先用conda装试试:

PackagesNotFoundError: The following packages are not available from current channels:

  - scienceplots

所以只能用pip安装:

pip install SciencePlots

装好之后还是报一样的错,觉得很奇怪,在其Pypi页面上看了一下:SciencePlots · PyPI

发现有这么几段话:

From version v1.1.0 on, import scienceplots is needed on top of your scripts so Matplotlib can make use of the styles.

Whenever you want to use it, simply add the following to the top of your python script:

import matplotlib.pyplot as plt
import scienceplots

plt.style.use('science')

You can also combine multiple styles together by:

plt.style.use(['science','ieee'])

所以解决方法就是在import matplotlib.pyplot as plt下面加上import scienceplots。

这个问题解决之后又碰到了新的问题,不停的报:

findfont: Generic family 'serif' not found because none of the following families were found: Times

发现这个问题GitHub上也有人讨论:findfont: Font family ['serif'] not found. Falling back to DejaVu Sans. · Issue #58 · garrettj403/SciencePlots · GitHub

解决方法就是这个链接里写的:findfont: Font family ['serif'] not found. Falling back to DejaVu Sans. · Issue #58 · garrettj403/SciencePlots · GitHub

像在我的电脑上的路径是:/home/quyu/anaconda3/envs/TranAD/lib/python3.8/site-packages/matplotlib/mpl-data/fonts,把下载的字体拷贝进去。然后删掉/home/quyu/.cache/matplotlib这个文件夹就可以了。

把这几个问题解决后,就可以输出论文里类似的结果图了(虽然我还是不太相信这个论文的代码和结论)。

你可能感兴趣的:(Python技巧,论文点评,python,开发语言)