Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: module 'matplotlib' has no attribute 'pyplot' #24

Open
stefan-kzmnv opened this issue Mar 19, 2023 · 2 comments
Open

AttributeError: module 'matplotlib' has no attribute 'pyplot' #24

stefan-kzmnv opened this issue Mar 19, 2023 · 2 comments

Comments

@stefan-kzmnv
Copy link

stefan-kzmnv commented Mar 19, 2023

While attempting to create a piano roll with librosa and matplotlib, like this:

def plot_piano_roll(pm, start_pitch, end_pitch, fs=100):
    # Use librosa's specshow function for displaying the piano roll
    librosa.display.specshow(pm.get_piano_roll(fs)[start_pitch:end_pitch],
                             hop_length=1, sr=fs, x_axis='time', y_axis='cqt_note',
                             fmin=pretty_midi.note_number_to_hz(start_pitch))

plt.figure(figsize=(8, 4))
plot_piano_roll(pm, 56, 70)

I encounter this error:

AttributeError                            Traceback (most recent call last)
File ~/anaconda3/lib/python3.9/site-packages/matplotlib_inline/backend_inline.py:100, in show(close, block)
     97 # only call close('all') if any to close
     98 # close triggers gc.collect, which can be slow
     99 if close and Gcf.get_all_fig_managers():
--> 100     matplotlib.pyplot.close('all')

File ~/anaconda3/lib/python3.9/site-packages/matplotlib/_api/__init__.py:226, in caching_module_getattr.<locals>.__getattr__(name)
    224 if name in props:
    225     return props[name].__get__(instance)
--> 226 raise AttributeError(
    227     f"module {cls.__module__!r} has no attribute {name!r}")

AttributeError: module 'matplotlib' has no attribute 'pyplot'

The error seems to be fixed if I explicitly declare import matplotlib.pyplot as plt at the start of the file and then use that to call it like this: plt.close('all'). If that alone is satisfactory, I would request to do a PR.

@QuLogic
Copy link

QuLogic commented Apr 21, 2023

The error seems to be fixed if I explicitly declare import matplotlib.pyplot as plt at the start of the file and then use that to call it like this: plt.close('all'). If that alone is satisfactory, I would request to do a PR.

This, or some variant of it, is the correct fix. Because matplotlib.pyplot is not explicitly imported, it may or may not exist at the time of this call, depending on what previous code did. The simplest change would be:

         if close and Gcf.get_all_fig_managers():
+            import matplotlib.pyplot
             matplotlib.pyplot.close('all')

@agucova
Copy link

agucova commented Jun 14, 2023

@QuLogic That suggested fix won't work, as in the original example the matplotlib module was overwritten silently by librosa, due to their implementation of lazy loading. See here for details.

In particular, as long as import matplotlib is present first, adding import matplotlib.pyplot won't work. However, it appears addingimport matplotlib.pyplot as plt does work (for reasons that surpass my mortal understanding).

While I think such an import should be added (as per your fix), the issue is really upstream with librosa, so I've filed an issue there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants