Unable to render plotnine graphic object (plotnine.ggplot)

Hello all,

I have been trying to use plotnine module to build a data info graph through Anaconda Code in Excel (desktop), but it doesn’t render in Excel. It just stays as a memory object (plotnine.ggplot) like this.

Screenshot 2025-07-13 153603

What am I missing here?

I have seen examples ( The BEST Data Visualizations for Data Science: Python in Excel Tutorial (Free File) - YouTube) where plotnine output can be rendered properly Python in Excel. Shouldn’t it also work in Anaconda Code?

Many thanks,
Andy

Environment:

Windows 11
Excel for M365 Desktop
Anaconda Code version: 1.2.1 (160)

I’m not sure how Python in Excel handles plotnine objects. It might have some deep inspection looking for matplotlib objects.

Anaconda Code is much more explicit, and unfortunately we don’t automatically handle plotnine objects – though we could add that in the future.

To show the result, we need it converted into a matplotlib figure using the .draw() method on the ggplot object.

The easiest way to do this is to add an object repr handler to your Imports and Definitions file

import plotnine

@repr_xl_value(plotnine.ggplot)
def ggplot_to_fig(x):
    return x.draw()

The repr_xl_value decorator will register plotnine.ggplot objects so the returned object is always run through this function before being sent to Excel.

Hi Jim,

Thank you so much for your response. I can confirm the .draw() function solved the problem. I was trying something like x.draw() and then use plt.show() for it and it didn’t work. I didn’t think of stopping at x.draw().

I really look forward to the day when plotnine is also supported in Anaconda Code, given that it is a elegant implantation of the powerful ggplot2 in R world.

I want to sincerely thank you and Anaconda Code/Toolbox team for your good work in bringing Python and R to Excel! I have been an avid user and it really is greatly promising. Keep up with the good work.

Andy

2 Likes