adtk icon indicating copy to clipboard operation
adtk copied to clipboard

Fix plot exception: 'seaborn-whitegrid' is not a valid package style, path of style file

Open csrgxtu opened this issue 1 year ago • 4 comments

when you use adtk==0.6.2 and plot the data, you will get following, that's because the matplotlib which is required by adtk installed don't have seaborn-whitegrid in recent versions. for styles available, you can refer: https://matplotlib.org/stable/gallery/style_sheets/style_sheets_reference.html

image

from adtk.visualization import plot
from matplotlib import style
plot(s_train) # s_train is loaded before.

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
File ~/.pyenv/versions/3.12.1/envs/ad/lib/python3.12/site-packages/matplotlib/style/core.py:137, in use(style)
    136 try:
--> 137     style = _rc_params_in_file(style)
    138 except OSError as err:

File ~/.pyenv/versions/3.12.1/envs/ad/lib/python3.12/site-packages/matplotlib/__init__.py:870, in _rc_params_in_file(fname, transform, fail_on_error)
    869 rc_temp = {}
--> 870 with _open_file_or_url(fname) as fd:
    871     try:

File ~/.pyenv/versions/3.12.1/lib/python3.12/contextlib.py:137, in _GeneratorContextManager.__enter__(self)
    136 try:
--> 137     return next(self.gen)
    138 except StopIteration:

File ~/.pyenv/versions/3.12.1/envs/ad/lib/python3.12/site-packages/matplotlib/__init__.py:847, in _open_file_or_url(fname)
    846 fname = os.path.expanduser(fname)
--> 847 with open(fname, encoding='utf-8') as f:
    848     yield f

FileNotFoundError: [Errno 2] No such file or directory: 'seaborn-whitegrid'

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

OSError                                   Traceback (most recent call last)
Cell In[2], line 3
      1 from adtk.visualization import plot
      2 from matplotlib import style
----> 3 plot(s_train)

File ~/.pyenv/versions/3.12.1/envs/ad/lib/python3.12/site-packages/adtk/visualization/_visualization.py:201, in plot(ts, anomaly, curve_group, ts_linewidth, ts_color, ts_alpha, ts_marker, ts_markersize, match_curve_name, anomaly_tag, anomaly_color, anomaly_alpha, anomaly_marker, anomaly_markersize, freq_as_period, axes, figsize, legend)
     35 """Plot time series and/or anomalies.
     36 
     37 Parameters
   (...)
    198 
    199 """
    200 # setup style
--> 201 plt.style.use("seaborn-whitegrid")
    203 # initialize color generator
    204 color_generator = ColorGenerator()

File ~/.pyenv/versions/3.12.1/envs/ad/lib/python3.12/site-packages/matplotlib/style/core.py:139, in use(style)
    137         style = _rc_params_in_file(style)
    138     except OSError as err:
--> 139         raise OSError(
    140             f"{style!r} is not a valid package style, path of style "
    141             f"file, URL of style file, or library style name (library "
    142             f"styles are listed in `style.available`)") from err
    143 filtered = {}
    144 for k in style:  # don't trigger RcParams.__getitem__('backend')

OSError: 'seaborn-whitegrid' 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`)

To use recent matplotlib versions, we need modify the logic in _visualization.plot, make it compatible with recent versions of matplotlib.

csrgxtu avatar Aug 01 '24 11:08 csrgxtu

after modify, test it, and got following: img_v3_02db_c674c7cd-3360-4316-8572-bb20cf52277g

csrgxtu avatar Aug 01 '24 11:08 csrgxtu

@tailaiw @roycoding plz help review this PR.

csrgxtu avatar Aug 01 '24 11:08 csrgxtu

Any chance this gets approved?

am2222 avatar Oct 18 '24 15:10 am2222

Can you guys merge it please?

tbenhamou avatar Feb 10 '25 08:02 tbenhamou