garrettj403 / SciencePlots

Matplotlib styles for scientific plotting
MIT License
6.96k stars 700 forks source link

OSError on 'science' as not a valid package style, path of style file, URL of style file, or library style name in v 2.0.1 #89

Closed dhakalnirajan closed 1 year ago

dhakalnirajan commented 1 year ago

Hi!

I loved using SciencePlots and I came back to Python after learning C, and many thanks for helping other folks out and me with the project!

I was using v 2.0.1 in Google Colab, and I cannot use the science style.

I did the following:

!pip install SciencePlots

import scienceplots
import matplotlib.pyplot as plt

plt.style.reload_library()

plt.style.use(['science', 'notebook', 'grid'])

And the generated error was the following:

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)
[/usr/local/lib/python3.9/dist-packages/matplotlib/style/core.py](https://localhost:8080/#) 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 "

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

How can I work on that issue?

Thanks!

echedey-ls commented 1 year ago

You don't have to reload the styles library. Remove plt.style.reload_library().

In the older versions, that was needed as SciencePlots copied the styles into Matplotlib default styles folder. After copying them you had to tell Matplotlib to update its styles. With the new version, importing SciencePlots registers new styles from the package folder, so if the library is reloaded after the import matplotlib statement, Matplotlib 'forgets' the SciencePlots ones.

Anyways, is that code from an example you found in current docs? I may have missed to update something.

dhakalnirajan commented 1 year ago

Hi @echedey-ls !

I tried the latest commit way using the code

!pip install git+https://github.com/garrettj403/SciencePlots

import scienceplots
import matplotlib.pyplot as plt

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

It worked.

Thanks!

If anyone is facing the error, you need to remove:

plt.style.reload_library()

if you have this code snippet as mpl removes the style from scienceplots library.