EDIT:
I found the answer (see below) how to align images inside my subnets:
for ax in axes: ax.set_anchor('W')
EDIT END
I have some data that I draw with imshow. It is long in the x direction, so I split it into several lines by plotting data slices in vertically stacked subheadings. I am pleased with the result, but for the last subtitle (not as wide as the others) I want to align it with others.
The code below is checked using Python 2.7.1 and matplotlib 1.2.x.
#! /usr/bin/env python import matplotlib.pyplot as plt import numpy as np x_slice = [0,3] y_slices = [[0,10],[10,20],[20,30],[30,35]] d = np.arange(35*3).reshape((35,3)).T vmin = d.min() vmax = d.max() fig, axes = plt.subplots(len(y_slices), 1) for i, s in enumerate(y_slices): axes[i].imshow( d[ x_slice[0]:x_slice[1], s[0]:s[1] ], vmin=vmin, vmax=vmax, aspect='equal', interpolation='none' ) plt.show()
leads to
Given Zhenyaโs hint, I played with the .get / set_position axis. I tried half the width, but I don't understand how this works.
for ax in axes: print ax.get_position() p3 = axes[3].get_position().get_points() x0, y0 = p3[0] x1, y1 = p3[1]
get_position
gives me the bbox of each subtitle:
for ax in axes: print ax.get_position() Bbox(array([[ 0.125 , 0.72608696], [ 0.9 , 0.9 ]])) Bbox(array([[ 0.125 , 0.5173913 ], [ 0.9 , 0.69130435]])) Bbox(array([[ 0.125 , 0.30869565], [ 0.9 , 0.4826087 ]])) Bbox(array([[ 0.125 , 0.1 ], [ 0.9 , 0.27391304]]))
therefore, all the subheadings have the same horizontal extent (from 0.125 to 0.9). Judging by the narrower fourth subtitle, the image inside the subtitle is somehow centered.
Take a look at the AxesImage objects:
for ax in axes: print ax.images[0] AxesImage(80,348.522;496x83.4783) AxesImage(80,248.348;496x83.4783) AxesImage(80,148.174;496x83.4783) AxesImage(80,48;496x83.4783)
again, the same horizontal extent for the 4th image too.
The following attempt to AxesImage.get_extent ():
for ax in axes: print ax.images[0].get_extent() # [left, right, bottom, top] (-0.5, 9.5, 2.5, -0.5) (-0.5, 9.5, 2.5, -0.5) (-0.5, 9.5, 2.5, -0.5) (-0.5, 4.5, 2.5, -0.5)
there is a difference (on the right), but the left value is the same for everyone, so why is the fourth centered then?
EDIT: they are all centered ...