Interactive 3D Matplotlib figure


I am having issues plotting an interactive 3D matlab figure. Plotting the figure directly using .show() works fine locally and to have this display on the streamlit site I need to be using st.plotly(fig) however, this is giving me errors.

Steps to reproduce

Code snippet:
Here is the code which creates the figure

def surf_plot(df, long, lat):

    fig, ax = plt.subplots(subplot_kw={"projection": "3d"})

    X = long
    Y = lat
    X, Y = np.meshgrid(X, Y)
    Z = df

    # Plot the surface.
    surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
                        linewidth=0, antialiased=False)

    # Customize the z axis.
    # A StrMethodFormatter is used automatically

    # Add a color bar which maps values to colors.
    fig.colorbar(surf, shrink=0.5, aspect=5)

    #export fig from function
    return fig

and simply calling as:


Expected behavior:

Iโ€™d expect to see a moveable, resizable, matlab surface plot which is what happens with however I get this error instead :

Actual behavior:

Traceback (most recent call last):
  File "C:\Users\name\miniconda3\lib\site-packages\streamlit\runtime\scriptrunner\", line 565, in _run_script
    exec(code, module.__dict__)
  File "C:\Users\name\OneDrive\OLR\python\streamlit\src\", line 231, in <module>
    app = application()
  File "C:\Users\name\OneDrive\OLR\python\streamlit\src\", line 142, in __init__
  File "C:\Users\name\miniconda3\lib\site-packages\streamlit\runtime\", line 311, in wrapped_func
    result = non_optional_func(*args, **kwargs)
  File "C:\Users\name\miniconda3\lib\site-packages\streamlit\elements\", line 163, in plotly_chart
  File "C:\Users\name\miniconda3\lib\site-packages\streamlit\elements\", line 198, in marshall
    figure =
  File "C:\Users\name\miniconda3\lib\site-packages\plotly\", line 111, in mpl_to_plotly
  File "C:\Users\name\miniconda3\lib\site-packages\plotly\matplotlylib\mplexporter\", line 53, in run
  File "C:\Users\name\miniconda3\lib\site-packages\plotly\matplotlylib\mplexporter\", line 124, in crawl_fig
  File "C:\Users\name\miniconda3\lib\site-packages\plotly\matplotlylib\mplexporter\", line 146, in crawl_ax
    self.draw_collection(ax, collection)
  File "C:\Users\name\miniconda3\lib\site-packages\plotly\matplotlylib\mplexporter\", line 289, in draw_collection
    offset_order = offset_dict[collection.get_offset_position()]
AttributeError: 'Poly3DCollection' object has no attribute 'get_offset_position'

I was looking into how to fix this error and came across this github repo but any suggestions in there did not work [mplexporter not getting Collection offset position due to matplotlib API changes in v3.5 ยท Issue #3624 ยท plotly/ ยท GitHub]. I then came across this: [st.pyplot - Streamlit Docs] and thought it might be something to do with the renderer and did the echo "backend: TkAgg" >> ~/.matplotlib/matplotlibrc command which now causes this error to be continuously outputted when I even run the app. First of all, how can I put whatever was in the matplotlibrc back to its default value so I have some functionality? Then how can I plot the interactive chart?

Debug info

  • Streamlit version: cannot check due to librc error, believe it is the most recent
  • Python version: 3.10.8
  • OS version: Windows 11
  • Browser version: Newest Chrome

Requirements file


  • uninstalling matplotlib and deleting the .matplotlib cache and then reinstalling fixed the echo issue. So I just need help with the interactive plot issue

st.plotly_chart expects a Plotly figure but you are creating a matplotlib figure. They are two different plotting libraries, and fortunately they have nothing to do with matlab :sweat_smile:. Check st.pyplot in order to display a matplotlib figure.

Oh, I am not sure why the code I was experimenting with/ my old code was uploaded. That error was being created with

st.pyplot(fig, clear_figure=True)

not the chart version which you mentioned. Thanks for pointing that out regardless