Displaying 3D-Plots with Gradio

Do either of the gr.Plot or gr.ScatterPlot components tolerate 3D plots to be passed as their ‘value’? I have created an Axes3D object using matplotlib.pyplot - but get an Attribute error (no attribute to_json) when I try to set this as the value for a gr.Plot. Ideally, in the end I will have a 3D plot in my gradio interface that is able to be physically manipulated - e.g. to achieve different viewing orientations, like a 3Dmodel.

The first component I tried was Model3D, but I’m not sure this is intended for simple 3D plots…

Could someone point me in the right direction?

Thanks!

Hi @dpraz !

May be a bug in gradio. Can you verify if what you’re returning from your function is an instance of matplotlib.figure.Figure? For reference this is the relevant python code that handles matplotlib plots


    def postprocess(self, y) -> dict[str, str] | None:
        """
        Parameters:
            y: plot data
        Returns:
            plot type mapped to plot base64 data
        """
        import matplotlib.figure

        if y is None:
            return None
        if isinstance(y, (ModuleType, matplotlib.figure.Figure)):  # type: ignore
            dtype = "matplotlib"
            out_y = processing_utils.encode_plot_to_base64(y)

Thank you @freddyaboulton for the quick reply. I’m not actually calling a function to generate this plot yet. First, I’m just trying to display a maneuverable 3D-plot. I do this by setting ‘value’ equal to my Axes3D object:

threeD= gr.Plot(value=ax, label="3-D Plot")

where ax is a perfectly functional 3D plot:

ax=plt.axes(projection="3d")

When I launch the interface (very basic at this stage), I get:

AttributeError: 'Axes3D' object has no attribute 'to_json.

Hope this helps - please let me know if my post can be improved (it is my first one).

Thanks for the reply @dpraz ! Is it possible for you to return a figure instead of an axes3d object?

Aha, yes @freddyaboulton this works - and I can now display the 3D plot as I would any 2D plot in a gr.Plot component. However, in doing so I lose the drag/turn functionality afforded by %matplotlib notebook.
I’ve made user-maneuverable plots in gradio before, although I was using plotly.express for those (albeit, they were 2D). Perhaps I’ll try that plotting package out to see if it resolves this new issue.

Thanks for your help!