def test_hbox_divider(): arr1 = np.arange(20).reshape((4, 5)) arr2 = np.arange(20).reshape((5, 4)) fig, (ax1, ax2) = plt.subplots(1, 2) ax1.imshow(arr1) ax2.imshow(arr2) pad = 0.5 # inches. divider = HBoxDivider( fig, 111, # Position of combined axes. horizontal=[Size.AxesX(ax1), Size.Fixed(pad), Size.AxesX(ax2)], vertical=[Size.AxesY(ax1), Size.Scaled(1), Size.AxesY(ax2)]) ax1.set_axes_locator(divider.new_locator(0)) ax2.set_axes_locator(divider.new_locator(2)) fig.canvas.draw() p1 = ax1.get_position() p2 = ax2.get_position() assert p1.height == p2.height assert p2.width / p1.width == pytest.approx((4 / 5)**2)
def make_heights_equal(fig, rect, ax1, ax2, pad): # pad in inches h1, v1 = Size.AxesX(ax1), Size.AxesY(ax1) h2, v2 = Size.AxesX(ax2), Size.AxesY(ax2) pad_v = Size.Scaled(1) pad_h = Size.Fixed(pad)
def make_heights_equal(fig, rect, ax1, ax2, pad): # pad in inches divider = HBoxDivider( fig, rect, horizontal=[Size.AxesX(ax1), Size.Fixed(pad), Size.AxesX(ax2)], vertical=[Size.AxesY(ax1), Size.Scaled(1), Size.AxesY(ax2)]) ax1.set_axes_locator(divider.new_locator(0)) ax2.set_axes_locator(divider.new_locator(2))
def make_heights_equal(fig, rect, ax1, ax2, pad): # pad in inches h1, v1 = Size.AxesX(ax1), Size.AxesY(ax1) h2, v2 = Size.AxesX(ax2), Size.AxesY(ax2) pad_v = Size.Scaled(1) pad_h = Size.Fixed(pad) my_divider = HBoxDivider(fig, rect, horizontal=[h1, pad_h, h2], vertical=[v1, pad_v, v2]) ax1.set_axes_locator(my_divider.new_locator(0)) ax2.set_axes_locator(my_divider.new_locator(2))
def _make_axes_grid(self): self.axes = self.fig.add_subplot(111) #Split up the current axes so there is space for a start and a stop button self.divider = make_axes_locatable(self.axes) pad = 0.01 # Padding between axes pad_size = Size.Fraction(pad, Size.AxesX(self.axes)) large_pad_size = Size.Fraction(0.1, Size.AxesY(self.axes)) #Define size of useful axes cells, 50% each in x 20% for buttons in y. small_x = Size.Fraction((1. - 2. * pad) / 10, Size.AxesX(self.axes)) ysize = Size.Fraction((1. - 2. * pad) / 15., Size.AxesY(self.axes)) #Set up grid, 3x3 with cells for padding. if self.num_buttons > 0: xsize = Size.Fraction((1. - 2. * pad) / self.num_buttons, Size.AxesX(self.axes)) horiz = [xsize] + [pad_size, xsize]*(self.num_buttons-1) + \ [Size.Fraction(0.1, Size.AxesY(self.axes)), small_x] vert = [ysize, pad_size] * self.num_sliders + \ [large_pad_size, large_pad_size, Size.AxesY(self.axes)] else: vert = [ysize, pad_size] * self.num_sliders + \ [large_pad_size, Size.AxesY(self.axes)] horiz = [Size.Fraction(0.8, Size.AxesX(self.axes))] + \ [Size.Fraction(0.1, Size.AxesX(self.axes))]*2 self.divider.set_horizontal(horiz) self.divider.set_vertical(vert) self.button_ny = len(vert) - 3 #If we are going to add a colorbar it will need an axis next to the plot if self.if_colorbar: nx1 = -3 self.cax = self.fig.add_axes((0., 0., 0.141, 1.)) locator = self.divider.new_locator(nx=-2, ny=len(vert) - 1, nx1=-1) self.cax.set_axes_locator(locator) else: #Main figure spans all horiz and is in the top (2) in vert. nx1 = -1 self.axes.set_axes_locator( self.divider.new_locator(nx=0, ny=len(vert) - 1, nx1=nx1))
import mpl_toolkits.axes_grid1.axes_size as Size from mpl_toolkits.axes_grid1 import Divider import matplotlib.pyplot as plt fig = plt.figure(figsize=(5.5, 4)) rect = (0.1, 0.1, 0.8, 0.8) ax = [fig.add_axes(rect, label="%d" % i) for i in range(4)] horiz = [Size.AxesX(ax[0]), Size.Fixed(.5), Size.AxesX(ax[1])] vert = [Size.AxesY(ax[0]), Size.Fixed(.5), Size.AxesY(ax[2])] divider = Divider(fig, rect, horiz, vert, aspect=False) ax[0].set_axes_locator(divider.new_locator(nx=0, ny=0)) ax[1].set_axes_locator(divider.new_locator(nx=2, ny=0)) ax[2].set_axes_locator(divider.new_locator(nx=0, ny=2)) ax[3].set_axes_locator(divider.new_locator(nx=2, ny=2)) ax[0].set_xlim(0, 2) ax[1].set_xlim(0, 1) ax[0].set_ylim(0, 1) ax[2].set_ylim(0, 2) divider.set_aspect(1.) for ax1 in ax: ax1.tick_params(labelbottom=False, labelleft=False) plt.show()
def add_controls(axes=None, slider=False): """ Adds Start/Stop controls to an axes having been given a animation instance. """ #If No axes specified use current axes. if not axes: axes = plt.gca() fig = axes.get_figure() #Split up the current axes so there is space for a start and a stop button divider = make_axes_locatable(axes) pad = 0.1 # Padding between axes pad_size = Size.Fraction(pad, Size.AxesX(axes)) #Define size of usefult axes cells, 50% each in x 20% for buttons in y. xsize = Size.Fraction((1. - 2. * pad) / 3., Size.AxesX(axes)) ysize = Size.Fraction((1. - 2. * pad) / 15., Size.AxesY(axes)) #Set up grid, 3x3 with cells for padding. divider.set_horizontal([xsize, pad_size, xsize, pad_size, xsize]) if slider: divider.set_vertical( [ysize, pad_size, ysize, pad_size, Size.AxesY(axes)]) bny = 2 else: divider.set_vertical([ysize, pad_size, Size.AxesY(axes)]) bny = 0 #Main figure spans all horiz and is in the top (2) in vert. axes.set_axes_locator( divider.new_locator(0, len(divider.get_vertical()) - 1, nx1=-1)) #Add two axes for buttons and make them 50/50 spilt at the bottom. bax1 = fig.add_axes((0., 0., 1., 1.)) locator = divider.new_locator(nx=0, ny=bny) bax1.set_axes_locator(locator) bax2 = fig.add_axes((0., 0., 0.8, 1.)) locator = divider.new_locator(nx=2, ny=bny) bax2.set_axes_locator(locator) bax3 = fig.add_axes((0., 0., 0.7, 1.)) locator = divider.new_locator(nx=4, ny=bny) bax3.set_axes_locator(locator) start = widgets.Button(bax1, "Start") stop = widgets.Button(bax2, "Stop") step = widgets.Button(bax3, "Step") #Make dummy refernce to prevent garbage collection bax1._button = start bax2._button = stop bax3._button = step if slider: bax4 = fig.add_axes((0., 0., 0.6, 1.)) locator = divider.new_locator(nx=0, ny=0, nx1=-1) bax4.set_axes_locator(locator) sframe = widgets.Slider(bax4, 'Frame', 0, 10, valinit=0, valfmt='%i') bax4._slider = sframe return axes, bax1, bax2, bax3, bax4 return axes, bax1, bax2, bax3
def _ColorBar(self): try: if self.options.plot_type is not 0 and self.options.justvector is not 1: try: ax = plt.gca() divider = make_axes_locatable(ax) try: if self.options.decimalcolorbar is not None: decimal = '%.' + str( self.options.decimalcolorbar) + 'f' else: decimal = None except Exception as ex: logging.info( ': Modulo Maps Functions : Error 012 : Failed to calculate decimacolorbar ' + ex) logging.shutdown() sys.exit() try: aux = np.float(self.options.scalarMax - self.options.scalarMin) if self.options.LogMap == 1: cba = plt.colorbar(self.pa) else: if self.options.colorbar == 1 or self.options.colorbar == 2: try: if self.options.scalarMin >= self.options.scalarMax: logging.info( ': Modulo Maps Functions : Error 013a : Scalar ´Min is bigger then Max. ' + ex) logging.shutdown() sys.exit() except Exception as ex: logging.info( ': Modulo Maps Functions : Error 013a : Scalar ´Min is bigger then Max. ' + ex) logging.shutdown() sys.exit() v2 = np.linspace( self.options.scalarMin, self.options.scalarMax, round( (aux / self.options.colorbarSpacing) + 1, 4)) if self.options.dynamic_limits == 1: v2 = np.linspace(self.options.scalarMin, self.options.scalarMax, 11) if len(v2) > 200: logging.info( ': Modulo Maps Functions : Error 013 : you requested more then 200 colorbar splits, please reduce' ) logging.shutdown() sys.exit() if self.options.orientationcolorbar == 'horizontal': width = axes_size.AxesX(ax, aspect=1 / 50) pad = axes_size.Fraction(4, width) cba = plt.colorbar( selfpa, ticks=v2, extend='max', spacing='proportional', orientation='horizontal', format=decimal) elif self.options.orientationcolorbar == 'vertical': width = axes_size.AxesY(ax, aspect=1 / 35) pad = axes_size.Fraction(0.55, width) if self.options.plot_type == 2: divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.05) cba = plt.colorbar( self.pa, cax=cax, ticks=v2, extend='max', spacing='proportional', orientation='vertical', format=decimal) elif self.options.plot_type == 1: width = axes_size.AxesY(ax, aspect=1 / 35) pad = axes_size.Fraction(0.55, width) cba = plt.colorbar( self.pa, cax=divider.append_axes("right", size=0.085, pad=pad), ticks=v2, extend='max', spacing='proportional', orientation='vertical', format=decimal) except Exception as ex: logging.info( ': Modulo Maps Functions : Error 013 : Failed to calculate width, pad and cba from colorbar. ' + ex) logging.shutdown() sys.exit() try: mpl.rcParams.update( {'font.size': self.options.fontsize}) if self.options.legend is not None and self.options.colorbar != 0: if self.options.plot_type == 2: cba.set_label(self.options.legend, fontsize=self.options.fontsize, rotation=270, labelpad=7) for t in cba.ax.get_yticklabels(): t.set_horizontalalignment('right') t.set_x(2.2) else: cba.set_label(self.options.legend, fontsize=self.options.fontsize) except Exception as ex: logging.info( ': Modulo Maps Functions : Error 014 : Failed to draw colorbar title. ' + ex) logging.shutdown() sys.exit() except Exception as ex: logging.info( ': Modulo Maps Functions : Error 015 : Failed inside colorbar manipulation ' ) logging.shutdown() sys.exit() return self except Exception as ex: logging.info( ': Modulo Maps Functions : Error 016 : Failed inside colorbar manipulation ' ) logging.shutdown() sys.exit()
if __name__ == "__main__": arr1 = np.arange(20).reshape((4, 5)) arr2 = np.arange(20).reshape((5, 4)) fig, (ax1, ax2) = plt.subplots(1, 2) ax1.imshow(arr1) ax2.imshow(arr2) pad = 0.5 # inches. divider = HBoxDivider( fig, 111, # Position of combined axes. horizontal=[Size.AxesX(ax1), Size.Fixed(pad), Size.AxesX(ax2)], vertical=[Size.AxesY(ax1), Size.Scaled(1), Size.AxesY(ax2)]) ax1.set_axes_locator(divider.new_locator(0)) ax2.set_axes_locator(divider.new_locator(2)) # annotate ax3 = plt.axes([0.5, 0.5, 0.001, 0.001], frameon=False) ax3.xaxis.set_visible(False) ax3.yaxis.set_visible(False) ax3.annotate( "Location of two axes are adjusted\n" "so that they have equal heights\n"
import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1 import make_axes_locatable, axes_size import numpy as np aspect = 55 pad_fraction = 0.5 ax = plt.gca() im = ax.imshow(np.arange(200).reshape((20, 10))) divider = make_axes_locatable(ax) width = axes_size.AxesY(ax, aspect=1. / aspect) height = axes_size.AxesX(ax, aspect=1.0 / 5) pad = axes_size.Fraction(pad_fraction, width) cax = divider.append_axes("right", size=width, pad=pad) plt.colorbar(im, cax=cax) plt.show()