TypeError: 'Figure' object is not callable in Getting Started example

I’ve started implementing the Getting Started example without using jupyter notebooks. I’m just using plain python command-line to run the code.

When attempting to plot the data, I get the error:
TypeError: 'Figure' object is not callable

when attempting to run plot_data.py.

Just end your file with f rather than f()

1 Like

Exactly what @carl said! :+1:

To get the matplotlib figure to display in a separate window you could try to add plt.show() at the end of your script (plt coming from the classic import matplotlib.pyplot as plt).

That didnt work. What did work was removing the creation of the figure and axis manually and replacing it with

    fig, ax = plt.subplots()

full working code:

from matplotlib.figure import Figure
import matplotlib.pyplot as plt
from matplotlib.backends.backend_agg import FigureCanvas
import numpy as np

def mpl_plot(avg, highlight):
    fig = Figure()
    FigureCanvas(fig)  # not needed in mpl >= 3.1
    ax = fig.add_subplot()
    if len(highlight): highlight.plot(style='o', ax=ax)
    return fig

def my_mpl_plot(avg, highlight):
    fig, ax = plt.subplots()
    if len(highlight): highlight.plot(style='o', ax=ax)
    return fig

def find_outliers(data, variable='Temperature', window=30, sigma=10, view_fn=mpl_plot):
    avg = data[variable].rolling(window=window).mean()
    residual = data[variable] - avg
    std = residual.rolling(window=window).std()
    outliers = (np.abs(residual) > std * sigma)
    return view_fn(avg, avg[outliers])

import load_data
f = find_outliers(load_data.data, variable='Temperature', window=20, sigma=10, view_fn=my_mpl_plot)
1 Like

Ok I see. plot.show() doesn’t work if the Figure hasn’t been created with the pyplot interface. In the getting started page there’s no need to use the pyplot interface because it runs in a notebook that has this line %matplotlib inline that turns the Figure representation into an actual plot.

The pyplot interface isn’t used in the getting started (and elsewhere) because of some potential memory leaks that could happen, see the reference documentation of the Matploblib pane:

Note that the examples below do not make use of the common matplotlib.pyplot API in order to avoid having to close the figure. If not closed, it could cause memory leaks and cause the inline backend to automatically display the figure. It is actually as simple as creating a matplotlib.Figure object, registering axes to this figure, and passing the figure to the Matplotlib pane that will take care of rendering it.

1 Like