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.

turtle
  • 7,533
  • 18
  • 68
  • 97

2 Answers2

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()
Community
  • 1
  • 1
kibitzforu
  • 383
  • 4
  • 10
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
Thomas K
  • 39,200
  • 7
  • 84
  • 86