Python "'module' object is not callable"

Question:

I’m trying to make a plot:

from matplotlib import *
import sys
from pylab import *

f = figure ( figsize =(7,7) )

But I get this error when I try to execute it:

  File "mratio.py", line 24, in <module>
    f = figure( figsize=(7,7) )
TypeError: 'module' object is not callable

I have run a similar script before, and I think I’ve imported all the relevant modules.

Asked By: ylangylang

||

Answers:

You need to do:

matplotlib.figure.Figure

Here,

matplotlib.figure is a package (module), and `Figure` is the method

Reference here.

So you would have to call it like this:

f = figure.Figure(figsize=(7,7))
Answered By: karthikr

The figure is a module provided by matplotlib.

You can read more about it in the Matplotlib documentation

I think what you want is matplotlib.figure.Figure (the class, rather than the module)

It’s documented here

from matplotlib import *
import sys
from pylab import *

f = figure.Figure( figsize =(7,7) )

or

from matplotlib import figure
f = figure.Figure( figsize =(7,7) )

or

from matplotlib.figure import Figure
f = Figure( figsize =(7,7) )

or to get pylab to work without conflicting with matplotlib:

from matplotlib import *
import sys
import pylab as pl

f = pl.figure( figsize =(7,7) )
Answered By: Ewan

To prevent future errors regarding matplotlib.pyplot, try doing this:
import matplotlib.pyplot as plt

If you use Jupyter notebooks and use: %matplotlib inline, make sure that the “%matplotlib inline FOLLOWS the import matplotlib.pyplot as plt

Karthikr’s answer works but might not eliminate errors in other lines of code related to the pyplot module.

Happy coding!!

Answered By: minion_1

Instead of

from matplotlib import *

use

import matplotlib.pyplot as plt
Answered By: Pirate X

For Jupyter notebook I solved this issue by importig matplotlib.pyplot instead.

import matplotlib.pyplot as plt
%matplotlib notebook

Making plots with f = plt.figure() now works.

Answered By: moijn001

Wrong Library:

import matplotlib as plt

plt.figure(figsize=(7, 7))

TypeError: 'module' object is not callable

But

Correct Library:

import matplotlib.pyplot as plt

plt.figure(figsize=(7, 7))

Above code worked for me for the same error.

Answered By: Awais
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.