Interactive 3D Matplotlib figure

Summary

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.
    ax.zaxis.set_major_locator(LinearLocator(10))
    # A StrMethodFormatter is used automatically
    ax.zaxis.set_major_formatter('{x:.02f}')

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

    #plt.show()

    #export fig from function
    return fig

and simply calling as:

st.plotly_chart(fig)

Expected behavior:

I’d expect to see a moveable, resizable, matlab surface plot which is what happens with plt.show() however I get this error instead :

Actual behavior:

Traceback (most recent call last):
  File "C:\Users\name\miniconda3\lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 565, in _run_script
    exec(code, module.__dict__)
  File "C:\Users\name\OneDrive\OLR\python\streamlit\src\testapp.py", line 231, in <module>
    app = application()
  File "C:\Users\name\OneDrive\OLR\python\streamlit\src\testapp.py", line 142, in __init__
    st.plotly_chart(olr_fig)
  File "C:\Users\name\miniconda3\lib\site-packages\streamlit\runtime\metrics_util.py", line 311, in wrapped_func
    result = non_optional_func(*args, **kwargs)
  File "C:\Users\name\miniconda3\lib\site-packages\streamlit\elements\plotly_chart.py", line 163, in plotly_chart
    marshall(
  File "C:\Users\name\miniconda3\lib\site-packages\streamlit\elements\plotly_chart.py", line 198, in marshall
    figure = plotly.tools.mpl_to_plotly(figure_or_data)
  File "C:\Users\name\miniconda3\lib\site-packages\plotly\tools.py", line 111, in mpl_to_plotly
    matplotlylib.Exporter(renderer).run(fig)
  File "C:\Users\name\miniconda3\lib\site-packages\plotly\matplotlylib\mplexporter\exporter.py", line 53, in run
    self.crawl_fig(fig)
  File "C:\Users\name\miniconda3\lib\site-packages\plotly\matplotlylib\mplexporter\exporter.py", line 124, in crawl_fig
    self.crawl_ax(ax)
  File "C:\Users\name\miniconda3\lib\site-packages\plotly\matplotlylib\mplexporter\exporter.py", line 146, in crawl_ax
    self.draw_collection(ax, collection)
  File "C:\Users\name\miniconda3\lib\site-packages\plotly\matplotlylib\mplexporter\exporter.py", 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/plotly.py · 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

matplotlib
streamlit

  • 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

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.