Alignment of stacked subnets

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

vertically stacked subplots, last centered

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] # [left, bottom, width, height] axes[3].set_position([x0, y0, (x1-x0)/2, y1-y0]) 

enter image description here

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 ...

+4
source share
3 answers

Axis.set_anchor still works (I just hope that I donโ€™t need to configure too much manually now):

 for ax in axes: ax.set_anchor('W') 

enter image description here

+6
source

You can control the position of the subtitle manually, for example:

 for ax in axes: print ax.get_position() 

and

 ax[3].set_position([0.1,0.2,0.3,0.4]) 

Alternatively, you may need GridSpec

+3
source

Often I find that ax.set_position very difficult to call accurate. I would prefer to use plt.subplots_adjust(wspace=0.005) # adjust the width between the subplots to adjust the distance between the two horizontal plt.subplots_adjust(wspace=0.005) # adjust the width between the subplots .

You can also adjust the vertical distance.

0
source

Source: https://habr.com/ru/post/1415953/


All Articles