Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib: get and set axes position

In matlab, it's straightforward to get and set the position of an existing axes on the figure:

  pos = get(gca(), 'position')
  set(gca(), 'position', pos)

How do I do this in Matplotlib?

I need this for two related reasons:

These are the specific problems I'm trying to solve:

  • I have a column of subplots where some have colorbars and some don't, and they aren't the same width i.e. the X axises don't align. The colorbar steals space from the axes. This also happens in matlab, and there I'd use the above trick to make all the axes equally wide by copying the width from an axes with a colorbar to those without.

  • add space between individual subplots by shrinkin an axes. The adjust_subplots() function adjusts all subplots the same.

like image 733
Åsmund Avatar asked Apr 17 '14 15:04

Åsmund


People also ask

How do I move axis in MatPlotLib?

MatPlotLib with Python To shift the Y-axis ticks from left to right, use ax. yaxis. tick_right() where ax is axis created using add_subplot(xyz) method.

What is POS MatPlotLib?

pos : [left, bottom, width, height] or Bbox. The new position of the in Figure coordinates.

What is BBOX in MatPlotLib?

BboxTransformTo is a transformation that linearly transforms points from the unit bounding box to a given Bbox. In your case, the transform itself is based upon a TransformedBBox which again has a Bbox upon which it is based and a transform - for this nested instance an Affine2D transform.


1 Answers

Setting axes position is similar in Matplotlib. You can use the get_position and set_position methods of the axes.

import matplotlib.pyplot as plt

ax = plt.subplot(111)
pos1 = ax.get_position() # get the original position 
pos2 = [pos1.x0 + 0.3, pos1.y0 + 0.3,  pos1.width / 2.0, pos1.height / 2.0] 
ax.set_position(pos2) # set a new position

You might also want to take a look at GridSpec if you haven't already.

like image 182
Molly Avatar answered Oct 09 '22 10:10

Molly