Matplotlib: Using a colorbar in conjunction with the extent keyword produces off-center images in figure window
I'm using matplotlib to do some plotting. I'd like to be able to use the extent keywo开发者_如何转开发rd to control the aspect ratio of my plots as well as display the size of the image along the axis tick marks.
My problem is that if I try to make an image that is longer in the row direction than it is in the column direction, and then I add in a colorbar the image shifts from the center of the figure canvas to the right side.
Some example code would be:
import numpy
from matplotlib import pyplot
pyplot.imshow(numpy.random.random( (100,100) ), extent = [0,50, 100, 0] )
pyplot.colorbar()
pyplot.show()
Is there a way to keep the image centered in the figure window? Is this a bug?
I am using the latest version from the github repository and Python 2.7 on Ubuntu 11.04
I don't know why this happen, but you can use cax argument of colorbar() to set the axe which contain the colorbar.
import numpy
from matplotlib import pyplot
pyplot.imshow(numpy.random.random( (100,100) ), extent = [0,50, 100, 0] )
cax = pyplot.axes([0.80, 0.1, 0.04, 0.8])
pyplot.colorbar(cax=cax)
pyplot.show()
精彩评论