python - alignment of stacked subplots -
edit:
i found myself answer (see below) how align images within subplots:
for ax in axes: ax.set_anchor('w') edit end
i have data plot imshow. it's long in x direction, break multiple lines plotting slices of data in vertically stacked subplots. happy result last subplot (not wide others) want left aligned others.
the code below tested python 2.7.1 , matplotlib 1.2.x.
#! /usr/bin/env python import matplotlib.pyplot plt import numpy 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) 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() results in

given tip zhenya played around axis.get/set_position. tried half width don't understand effect has
for ax in axes: print ax.get_position() p3 = axes[3].get_position().get_points() x0, y0 = p3[0] x1, y1 = p3[1] # [left, bottom, width, height] axes[3].set_position([x0, y0, (x1-x0)/2, y1-y0]) 
get_position gives me bbox of each subplot:
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]])) so subplots have exact same horizontal extent (0.125 0.9). judging narrower 4th subplot image inside subplot somehow centered.
let's @ 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, same horizontal extent 4th image too.
next try 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 difference (right) left value same why 4th 1 centered then?
edit: centered...
axis.set_anchor works far (i hope don't have adjust manually now):
for ax in axes: ax.set_anchor('w') 
Comments
Post a Comment