Skip to content
Advertisement

Why is ‘figure’ object not callable in nested function?

I’m calling a function that creates a matplotlib figure with two subplots and then plots subplots with nested functions. It looks like this basically:

def combined_plot(df_1, df_2):
    
    fig, ax = plt.subplots(1, 2, figsize=(14,4))
    plot_function_one(df_1, ax[0])
    plot_function_two(df_2, ax[1])
    
    return fig

When I call, this I get this type error: 'Figure' object is not callable. I’ve done this kind of thing in other contexts before, so I’m curious what potential causes are, or what exactly this error means at a highly level. When I google the error in the context of matplotlib I don’t get any relevant results.

Here’s the whole error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-66-aeb1feab3628> in <module>
      4 subplot1_df = wrangling_subplot1_df(df2)
      5 subplot2_df, _ = wrangling_subplot2_df(df2)
----> 6 fig2 = combined_cpu_plot(subplot1_df, subplot2_df)

TypeError: 'Figure' object is not callable

Note: It’s not really possible for me to provide an example that’s reproducible because the code and data so complex there’s almost no way for me to guess what amount of simiplication will reporduce the error. So I’m hoping there’s something wrong with what I’m doing at this high-level — if not, that’s helpful in and of itself.

I can run the body of the function outside the function call without getting the error, so it seems the error is coming from putting these calls in the function. If I run the function once, there’s no error. If I run it a second time, it throws the error, and continues throwing it until I go up and reset the functions. If I run it twice in a row, it throws an error. So if I run, this, I get the figure:

subplot1_df = wrangling_subplot1_df(df1)
subplot2_df, _ = wrangling_subplot2_df(df1)
fig1 = combined_cpu_plot(subplot1_df, subplot2_df)

But if I run this, I get the type error:

subplot1_df = wrangling_subplot1_df(df1)
subplot2_df, _ = wrangling_subplot2_df(df1)
fig1 = combined_cpu_plot(subplot1_df, subplot2_df)

subplot1_df = wrangling_subplot1_df(df2)
subplot2_df, _ = wrangling_subplot2_df(df2)
fig2 = combined_cpu_plot(subplot1_df, subplot2_df)

So I’m guessing something is getting left behind that’s confusing the function the second time around? I tried pyplot.close(), but that didn’t seem to affect the issue.

Edit: Added whole error.

Edit: Added example about running a second time.

Advertisement

Answer

Don’t assign your function to a variable with the same name! Somewhere in my code, I had assigned the figure that the function produced to a same-named variable like this:

combined_cpu_plot = combined_cpu_plot(subplot1_df, subplot2_df)

This works fine the first time around, since the name combined_cpu_plot now contains the matplotlib figure returned by the function. However, the second time around, the syntax tries to call the figure as a function, which is why the error says the figure object isn’t callable.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement