Take the 2-minute tour ×

I am using code written by a former employee. In this code, the person performs a plot on Python using Qt. I'm trying to add a colorbar and contour maps, but to no avail. Here's part of the code that's relevant.

from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas

fig = Figure()
axes = fig.add_subplot(111)
canvas = FigureCanvas(fig)

axes.imshow(data_and_extents, interpolation='bilinear')

#TODO: Add colorbar and contour maps with some values

I have tried axes.colorbar() to no avail. How do I add colorbars and contour maps to surface plots set up like this?

Thanks SOCommunity!

share|improve this question
First error: First line should be from matplotlib.figure import Figure –  halex Aug 31 '12 at 18:08
Corrected. Still not working though :( I'll edit the code to avoid further confusion. –  eqb Aug 31 '12 at 18:09
It should be fig.colorbar instead of axes.colorbar –  halex Aug 31 '12 at 18:15
Thanks! The issue now is this: colorbar() takes at least 1 argument (none given). I'm wondering how to create a mappable object now per this guide -- matplotlib.sourceforge.net/api/… –  eqb Aug 31 '12 at 18:23
See this example: matplotlib.sourceforge.net/examples/pylab_examples/… It should help you :) -> cbar = fig.colorbar(axes) –  halex Aug 31 '12 at 18:24
show 2 more comments

1 Answer

The first error is in the first line:

from matplotlib.figure import Figure

is the correct one. Next: Instead of axes.colorbar it's fig.corbar. With your variable names you should be able to create a colorbar with:

cbar = fig.colorbar(axes)

See this example for more.

share|improve this answer
add comment

Your Answer


