8

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.

9
  • have you shadowed figure with something else? Commented May 13, 2013 at 16:50
  • run import pylab; print pylab.__file__ and give us the result Commented May 13, 2013 at 16:52
  • /home/apps/fas/Langs/Python/2.7.2/lib/python2.7/site-packages/matplotlib/pylab.pyc Commented May 13, 2013 at 17:36
  • How about print pylab.figure? Commented May 14, 2013 at 1:36
  • Also what are the conents of "/home/apps/fas/Langs/Python/2.7.2/lib/python2.7/site-packages/matplotlib/pylab.p‌​y"? Commented May 14, 2013 at 1:41

6 Answers 6

13

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) )
Sign up to request clarification or add additional context in comments.

6 Comments

But figure should be a function from pylab and so this should still work.
@WinstonEwert I've added another solution to allow pylab to import figure without conflicting with matplotlib
But that doesn't explain why figure from pylab didn't replace matplotlib's figure. It should have.
I was wondering the same. I've plotted figures before without calling "figure.Figure".
Using the code snippet on it's own works with Python v3.5, however if I change the import order, loading pylab before matplotlib, I get the error. So the order is important. Best to explicitly specify pl.figure as suggested by @Ewan to avoid this
|
3

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))

1 Comment

The solution of @minion_1 is the actual solution, this here is 2013 solution.
3

Instead of

from matplotlib import *

use

import matplotlib.pyplot as plt

1 Comment

This has been stated before by minion_1. Still you offer a clearly formatted advice for the change.
2

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!!

Comments

1

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.

Comments

0

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.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.