[Matplotlib-users] Understanding axes position

Jody Klymak jklymak at uvic.ca
Wed Sep 20 14:07:38 EDT 2017


Hi Nunzio,

The issue is that you call `ax.set_aspect(‘equal’)`, and that 
changes the axes box.  However, `ax.get_position` returns the `frozen` 
position, i.e. the old position.  I don’t see a method to return the 
new position (and I wonder why `get_position` behaves this way).

As a work around, you can get the aspect-ratio changed position as:

```python
figW, figH = fig.get_size_inches()
fig_aspect = figH / figW
newpos = pp.shrunk_to_aspect(ax.get_aspect() * ax.get_data_ratio(), pp, 
fig_aspect).anchored(ax.get_anchor(), pp))
```

which I appreciate is a bit of a pain….

Cheers,   Jody




On 20 Sep 2017, at 7:06, Nunzio Losacco wrote:

> Hi all,
>
> what I’m trying to do here is having third plot with its base 
> aligned with the others and with reduced height (the final aim is 
> custom positioning a colorbar).
> As you can see I’m getting the wrong position. What am I missing?
>
> Thanks for any help
>
> NL
>
> # -*- coding: utf-8 -*-
>
> from __future__ import unicode_literals
> import numpy as np
> from scipy.interpolate import griddata
> import matplotlib
> import matplotlib.pyplot as plt
> from matplotlib.patches import Circle
> from matplotlib.pylab import cm
> import matplotlib.colors as colors
> from mpl_toolkits.axes_grid1 import make_axes_locatable, axes_size
>
> matplotlib.rcParams.update({'font.size': 8})
>
> fig = plt.figure()
> fig.set_size_inches(6.3,6.3)
>
> ax1 = plt.subplot(111)
> divider = make_axes_locatable(ax1)
> ax2 = divider.append_axes('right', size='100%', pad=0.3)
>
> axes = [ax1, ax2]
> ltypes = ['dashed', 'solid']
>
> xi = np.linspace(-18.125, 18.125, 11)
> yi = np.linspace(0, 28, 9)
> xv, yv = np.meshgrid(xi, yi)
>
> xcOdd = 0.2
> zcOdd = 0.725
> xcEven = 0.6
> zcEven = 0.725
>
> maskRadius = 0.15
>
> for i in range(2):
>     ax = axes[i]
>     ax.set_xlabel('distance [m]')
>     if i == 0:
>         ax.set_ylabel('depth [m]')
>     if i == 1:
>         ax.set_yticklabels([])
>     ax.invert_yaxis()
>     ax.tick_params(direction='in')
>     ax.set_aspect('equal')
>     odd = Circle((xcOdd, zcOdd), .15, linewidth=1.2, color='k', 
> fill=False)
>     even = Circle((xcEven, zcEven), .15, linewidth=1.2, 
> linestyle=ltypes[i], color='k', fill=False)
>
>     vmax = 15.
>     vmin =  0.
>     norm = matplotlib.colors.Normalize(vmin,vmax, clip=False)
>
>     color_map = 
> matplotlib.colors.ListedColormap(plt.cm.Greys(np.linspace(0.25, 1, 
> 5)), "name")
>
>     ax.add_patch(odd)
>     ax.add_patch(even)
>
> pad = 0.03
> width = 0.03
>
> pos = ax2.get_position()
>
> ax3 = fig.add_axes([pos.xmax + pad, pos.ymin, width, 
> 0.7*(pos.ymax-pos.ymin) ])
>
> plt.savefig('prova-vect-paper-test-2.eps', format='eps')
>
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users at python.org
> https://mail.python.org/mailman/listinfo/matplotlib-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20170920/82c2953a/attachment.html>


More information about the Matplotlib-users mailing list