1

I have a strange problem with matplotlib that I can not seem to figure out. When using the ipython notebook with the pylab flag, ipython notebook --pylab inline I have a line of code that looks like this that is used to generate a colorbar with matplotlib:

im = ax.imshow(df, vmin=vmin, vmax=vmax)

The code works correctly and I get a nice colorbar. When I run this code as a python file I get an error, NameError: name 'ax' is not defined. I understand that the ipython notebook --pylab inline automatically imports a bunch of stuff into the notebook, but I cannot figure out what I need to import to fix the problem. print type(ax) gives:

<class 'matplotlib.axes.AxesSubplot'>

Can anyone point out why my code works in ipython but not a plain python file? Thanks in advance.

2 Answers 2

5

I had the same problem. Per this entry: How to abbreviate xtick labels years to 2 digits in a matplotlib plot

try defining 'ax' by adding (before the line causing the error):

ax = plt.gca()
Sign up to request clarification or add additional context in comments.

Comments

2

I'm not quite sure what you've done, because aX isn't defined by default as part of pylab.

Normally, ax refers to an axis object. There are a few ways you can get one:

matplotlib.pyplot.gca()            # gca = get current axis
matplotlib.pyplot.subplot(2,1,1)   # For creating multiple plots in one figure
fig.get_axes()[x]                  # Where fig is a Figure object

Comments

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.