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

Summary

I am trying to run the same st.pyplot code and get this error: AttributeError: module ‘matplotlib’ has no attribute ‘axes’.

Steps to reproduce

Code snippet:
import streamlit
import numpy as np
import matplotlib.pyplot as plt
arr = np.random.normal(1, 1, size=100)
fig, ax = plt.subplots()
ax.hist(arr, bins=20)
st.pyplot(fig)

I have matplotlib v3.7.2 installed

Expected behavior:

the chart should render

Actual behavior:

File "/Users/matthewburd/Python/AudioAnalysis/audio_analysis.py", line 121, in main
    fig, ax = plt.subplots()
              ^^^^^^^^^^^^^^File "/opt/homebrew/lib/python3.11/site-packages/matplotlib/pyplot.py", line 1502, in subplots
    axs = fig.subplots(nrows=nrows, ncols=ncols, sharex=sharex, sharey=sharey,
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "/opt/homebrew/lib/python3.11/site-packages/matplotlib/figure.py", line 917, in subplots
    axs = gs.subplots(sharex=sharex, sharey=sharey, squeeze=squeeze,
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "/opt/homebrew/lib/python3.11/site-packages/matplotlib/gridspec.py", line 299, in subplots
    axarr[row, col] = figure.add_subplot(
                      ^^^^^^^^^^^^^^^^^^^File "/opt/homebrew/lib/python3.11/site-packages/matplotlib/figure.py", line 750, in add_subplot
    and isinstance(args[0], mpl.axes._base._AxesBase)
                            ^^^^^^^^File "/opt/homebrew/lib/python3.11/site-packages/matplotlib/_api/__init__.py", line 226, in __getattr__
    raise AttributeError(

Debug info

  • Streamlit version: 1.23.1python

  • Python version: 3.9.6

  • OS version: MacOS Ventura 13.1

  • Browser version: Safari 16.2

Hey @Matt_Burd,

Thanks for sharing your question!

Have you imported streamlit in your app? Your code snippet excludes that import, and your app does run without errors for me once I add that line.

yes, I neglected to show that. Streamlit is working apart from that plotting issue

I just got a similar error with completely different packages (ndx-sound). I think it might be a version control issue, so if you end up finding a solution, I’d be happy to hear what it was!
Haven’t found the error message anywhere else online…