I think your question is essentially: "How does matplotlib's state machine interface work?". I'll answer that in a second.
However, let's back up and answer the import question first.
Namespaces and import from
Say we have a file name foo.py
with the following contents:
DUMMY = 1
def talk():
print DUMMY
And then we do:
from foo import talk
talk()
The function will still work as expected, even though DUMMY
is nowhere in the namespace that we're working in.
If we do:
print DUMMY
We'll get a NameError
.
Instead on injecting things into the current namespace, each function keeps track of the namespace in which it was created. In this case:
assert 'DUMMY' in talk.__globals__
With that out of the way, let's get back to matplotlib
The matplotlib
State Machine
Matplotlib's pyplot
interface uses a state machine to keep track of the currently active figure, axes, image, etc. It's meant to directly mirror Matlab's approach for an easier transition from Matlab to Python.
However, you'll typically use an object-oriented approach when using matplotlib
, and limit the usage of pyplot
to a couple of convenience functions: plt.subplots()
or plt.figure()
and plt.show()
.
Let's break down a simple example using only the pyplot
interface and explain the steps that matplotlib takes.
For example, if we do something like:
import matplotlib.pyplot as plt
plt.plot(range(10))
plt.show()
What happens? plt.plot
is basically a one-liner. It's return plt.gca().plot(*args, **kwargs)
. Essentially, it boils down to this:
plt.plot
tries to get the current Axes
instance.
- Because there hasn't been one created yet, it creates a
Figure
and adds an Axes
to it.
- The
Axes
instance's plot
method is called and a line is added to the axes (the line object is also returned).
- The gui mainloop for the
Figure
is entered when we call show
If all this seems a bit indirect, it is. That's why shouldn't be using that style. Instead, use the standard object-oriented interface. That way it's much clearer what Axes you're plotting on, which Figure you're saving, etc.
With that in mind, let's back up and use the more common style instead of the matlab-esque plt.plot
:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(range(10))
plt.show()
Here, we've used a few pyplot
functions to create a new figure/axes and display the figure. You can skip them entirely, but it gets a touch verbose.
Basically:
subplots
creates a new Figure
instance (fig
) and adds one or more Axes
(subplots) to the figure. By default, a single Axes (ax
) will be added to the figure.
- We add a line to the
Axes
instance by calling its plot
method (ax.plot(data)
).
- We enter the gui mainloop with
plt.show()
.
Hopefully that clarifies things a touch. If you're still struggling, you might have a look through the documentation or a few other SO questions. For example: