pythonmatplotlibjupytermodulenotfounderror

ModuleNotFoundError: No module named 'matplotlib.axes._subplots' on Windows and Jupyter


I hope this question is not a dublicate. The answers in other thrats dosn't help me.

I have installed Python 3.12
on a Windows laptop/notebook (Windows 11 Home, 10.0.22631 on a Dell G15 5520, x64).
For my project i use Jupyter.
I have pip and Python in PATH (pip-commands and pip-jupyter-commands are working well).

I use following packages in my project so far:

from timeseries_generator import LinearTrend, Generator, WhiteNoise, RandomFeatureFactor #(installed directly from GIT with: pip install git+https://github.com/Nike-Inc/timeseries-generator.git, or pip install jupyter git+https://github.com/Nike-Inc/timeseries-generator.git
import pandas as pd
import matplotlib #(because of the error I tried to install seperately)
import keras #(for machine learning later on)

When I run the code (from the examples of timeseriesgenerator, just to try out timesieries_generator) I get following error:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[3], line 1
----> 1 from timeseries_generator import LinearTrend, Generator, WhiteNoise, RandomFeatureFactor
      2 import pandas as pd
      3 import matplotlib

File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\timeseries_generator\__init__.py:1
----> 1 from .base_factor import BaseFactor
      2 from .errors import *
      3 from .generator import Generator

File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\timeseries_generator\base_factor.py:5
      2 from typing import List, Dict, Optional, Union, Tuple
      4 from matplotlib.figure import Figure
----> 5 from matplotlib.axes._subplots import SubplotBase
      6 from matplotlib.pyplot import subplots
      7 from pandas import DataFrame, date_range, DatetimeIndex

ModuleNotFoundError: No module named 'matplotlib.axes._subplots'

So it seems as it is a problem with timeseries_generator.

I tried a lot of recommondations from the internet (have just one Python installed, updated everyting, install matplotlib by itself, even install it directly in jupyter as recommeded here...).

When I install matplotlib (pip install matplotlib, pip install jupyter matplotlib), I get following request:

Requirement already satisfied: types-python-dateutil>=2.8.10 in c:PATH-To-Python\python312\lib\site-packages (from arrow>=0.15.0->isoduration->jsonschema[format-nongpl]>=4.18.0->jupyter-events>=0.9.0->jupyter-server<3,>=2.4.0->notebook->jupyter) (2.9.0.20240316)  

in various forms.

The python code in Jupyter looks as following:

from timeseries_generator import LinearTrend, Generator, WhiteNoise, RandomFeatureFactor
import pandas as pd
import matplotlib
import keras

# setting up a linear trend
lt = LinearTrend(coef=2.0, offset=1., col_name="my_linear_trend")
g = Generator(factors={lt}, features=None, date_range=pd.date_range(start="01-01-2020", end="01-20-2020"))
g.generate()
g.plot()

# update by adding some white noise to the generator
wn = WhiteNoise(stdev_factor=0.05)
g.update_factor(wn)
g.generate()
g.plot()

I want to get some synthetic graphs (rendom plots/graphs) for machine learning.

I hope you can help me. Thank you for your answers in advance!


Solution

  • I think the issue is that the timeseries-generator package is not compatible with the latest version of Matplotlib. For it to work, you'll need to downgrade your Matplotlib to a version that still contains the matplotlib.axes._subplots module - it seems that v3.6.3 was the last compatible version, so do:

    pip install matplotlib==3.6.3
    

    Having said that, I don't think Matplotlib v3.6.3 is available for Python 3.12. Matplotlib v3.7 seems to be the oldest version for which there is a pip installable version from PyPI for Python 3.12.

    So, overall, if you want to use the pip installable timeseries-generator, you will have to switch to using Python 3.11 and Matplotlib v3.6.3.

    As suggested in the comments, you might want to think about using a virtual environment/package manager such as anaconda, to allow different Python versions and install certain package versions in a contained way.

    Another hacky solution (not recommended if you want to have reproducible code, but "ok" for a one-off situation) is to directly edit your local installation of timeseries-generator. In particular, change line 5 of ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\timeseries_generator\base_factor.py to be:

    from matplotlib.axes import SubplotBase
    

    which should work with the latest Matplotlib version. You could alternatively clone the git repository, edit the file there, and install the package directly from the clone. I cannot guarantee that there won't be other Python 3.12 incompatibilities though.