def demo_locatable_axes_hard(fig): from mpl_toolkits.axes_grid1 import SubplotDivider, Size from mpl_toolkits.axes_grid1.mpl_axes import Axes divider = SubplotDivider(fig, 2, 2, 2, aspect=True) # axes for image ax = Axes(fig, divider.get_position()) # axes for colorbar ax_cb = Axes(fig, divider.get_position()) h = [ Size.AxesX(ax), # main axes Size.Fixed(0.05), # padding, 0.1 inch Size.Fixed(0.2), # colorbar, 0.3 inch ] v = [Size.AxesY(ax)] divider.set_horizontal(h) divider.set_vertical(v) ax.set_axes_locator(divider.new_locator(nx=0, ny=0)) ax_cb.set_axes_locator(divider.new_locator(nx=2, ny=0)) fig.add_axes(ax) fig.add_axes(ax_cb) ax_cb.axis["left"].toggle(all=False) ax_cb.axis["right"].toggle(ticks=True) Z, extent = get_demo_image() im = ax.imshow(Z, extent=extent, interpolation="nearest") plt.colorbar(im, cax=ax_cb) plt.setp(ax_cb.get_yticklabels(), visible=False)
def addColorbar(mappable, ax): """ Append colorbar to axes Parameters ---------- mappable : a mappable object ax : an axes object Returns ------- cbax : colorbar axes object Notes ----- This is mostly useful for axes created with :func:`curvedEarthAxes`. written by Sebastien, 2013-04 """ from mpl_toolkits.axes_grid1 import SubplotDivider, LocatableAxes, Size import matplotlib.pyplot as plt fig1 = ax.get_figure() divider = SubplotDivider(fig1, *ax.get_geometry(), aspect=True) # axes for colorbar cbax = LocatableAxes(fig1, divider.get_position()) h = [ Size.AxesX(ax), # main axes Size.Fixed(0.1), # padding Size.Fixed(0.2) ] # colorbar v = [Size.AxesY(ax)] _ = divider.set_horizontal(h) _ = divider.set_vertical(v) _ = ax.set_axes_locator(divider.new_locator(nx=0, ny=0)) _ = cbax.set_axes_locator(divider.new_locator(nx=2, ny=0)) _ = fig1.add_axes(cbax) _ = cbax.axis["left"].toggle(all=False) _ = cbax.axis["top"].toggle(all=False) _ = cbax.axis["bottom"].toggle(all=False) _ = cbax.axis["right"].toggle(ticklabels=True, label=True) _ = plt.colorbar(mappable, cax=cbax) return cbax
def plot_heatmap(fig2, Z): from mpl_toolkits.axes_grid1 \ import SubplotDivider, LocatableAxes, Size Z = np.flipud(Z) divider = SubplotDivider(fig2, 1, 1, 1, aspect=True) # axes for image ax = LocatableAxes(fig2, divider.get_position()) # axes for colorbar ax_cb = LocatableAxes(fig2, divider.get_position()) h = [ Size.AxesX(ax), # main axes Size.Fixed(0.05), # padding, 0.1 inch Size.Fixed(0.2), # colorbar, 0.3 inch ] v = [Size.AxesY(ax)] divider.set_horizontal(h) divider.set_vertical(v) ax.set_axes_locator(divider.new_locator(nx=0, ny=0)) ax_cb.set_axes_locator(divider.new_locator(nx=2, ny=0)) fig2.add_axes(ax) fig2.add_axes(ax_cb) ax_cb.axis["left"].toggle(all=False) ax_cb.axis["right"].toggle(ticks=True) im = ax.imshow(Z, cmap=cm.coolwarm, extent=(0, 1, 0, 1), interpolation="nearest") plt.colorbar(im, cax=ax_cb) plt.setp(ax_cb.get_yticklabels(), visible=False) mngr = plt.get_current_fig_manager() geom = mngr.window.geometry() x, y, dx, dy = geom.getRect() mngr.window.setGeometry(dx + 200, 100, dx, dy)
def plot2dHeatMap(fig, x, y, z): '''z is a 2d grid; x and y are implicit linspaces''' from mpl_toolkits.axes_grid1 \ import SubplotDivider, LocatableAxes, Size z = np.flipud(z) divider = SubplotDivider(fig, 1, 1, 1, aspect=True) # axes for image ax = LocatableAxes(fig, divider.get_position()) # axes for colorbar ax_cb = LocatableAxes(fig, divider.get_position()) h = [ Size.AxesX(ax), # main axes Size.Fixed(0.05), # padding, 0.1 inch Size.Fixed(0.2), # colorbar, 0.3 inch ] v = [Size.AxesY(ax)] divider.set_horizontal(h) divider.set_vertical(v) ax.set_axes_locator(divider.new_locator(nx=0, ny=0)) ax_cb.set_axes_locator(divider.new_locator(nx=2, ny=0)) fig.add_axes(ax) fig.add_axes(ax_cb) ax_cb.axis["left"].toggle(all=False) ax_cb.axis["right"].toggle(ticks=True) im = ax.imshow(z, cmap=cm.coolwarm, extent=(0, 1, 0, 1), interpolation="nearest") plt.colorbar(im, cax=ax_cb) plt.setp(ax_cb.get_yticklabels(), visible=False) return ax
def add_cbar(mappable, ax): """ Append colorbar to axes Copied from DaViTPy: https://github.com/vtsuperdarn/davitpy/blob/1b578ea2491888e3d97d6e0a8bc6d8cc7c9211fb/davitpy/utils/plotUtils.py#L674 """ from mpl_toolkits.axes_grid1 import SubplotDivider, Size from mpl_toolkits.axes_grid1.mpl_axes import Axes import matplotlib.pyplot as plt fig1 = ax.get_figure() divider = SubplotDivider(fig1, *ax.get_geometry(), aspect=True) # axes for colorbar cbax = Axes(fig1, divider.get_position()) h = [ Size.AxesX(ax), # main axes Size.Fixed(0.1), # padding Size.Fixed(0.2) ] # colorbar v = [Size.AxesY(ax)] _ = divider.set_horizontal(h) _ = divider.set_vertical(v) _ = ax.set_axes_locator(divider.new_locator(nx=0, ny=0)) _ = cbax.set_axes_locator(divider.new_locator(nx=2, ny=0)) _ = fig1.add_axes(cbax) _ = cbax.axis["left"].toggle(all=False) _ = cbax.axis["top"].toggle(all=False) _ = cbax.axis["bottom"].toggle(all=False) _ = cbax.axis["right"].toggle(ticklabels=True, label=True) _ = plt.colorbar(mappable, cax=cbax) return cbax
def demo_locatable_axes_hard(fig): from mpl_toolkits.axes_grid1 import SubplotDivider, Size from mpl_toolkits.axes_grid1.mpl_axes import Axes divider = SubplotDivider(fig, 2, 2, 2, aspect=True) # axes for image ax = fig.add_axes(divider.get_position(), axes_class=Axes) # axes for colorbar # (the label prevents Axes.add_axes from incorrectly believing that the two # axes are the same) ax_cb = fig.add_axes(divider.get_position(), axes_class=Axes, label="cb") h = [ Size.AxesX(ax), # main axes Size.Fixed(0.05), # padding, 0.1 inch Size.Fixed(0.2), # colorbar, 0.3 inch ] v = [Size.AxesY(ax)] divider.set_horizontal(h) divider.set_vertical(v) ax.set_axes_locator(divider.new_locator(nx=0, ny=0)) ax_cb.set_axes_locator(divider.new_locator(nx=2, ny=0)) ax_cb.axis["left"].toggle(all=False) ax_cb.axis["right"].toggle(ticks=True) Z, extent = get_demo_image() im = ax.imshow(Z, extent=extent) plt.colorbar(im, cax=ax_cb) ax_cb.yaxis.set_tick_params(labelright=False)
def __init__(self, ax, graph, select_opts): self._ax = ax self._graph = graph self._select_opts = select_opts if self._select_opts.has_legend: self._legend = self._select_opts._legend else: self._legend = None self._vertex_opts = VertexOptions(graph) pad = Size.Fixed(0.1) graph_width = Size.AxesX(graph.ax) self.menu_width = Size.Fraction(0.35, Size.AxesX(graph.ax)) self.n_rows = 13 self.cell_height = Size.Fraction(0.97 / 10.0, Size.AxesY(graph.ax)) divider = make_axes_locatable(ax) divider.set_horizontal([graph_width, pad, self.menu_width]) if self._legend is not None: leg_span = self._get_span(self._legend.ax) leg_top, leg_bottom = self.n_rows, self.n_rows - leg_span menu_layout = [self.cell_height] * leg_span + [pad] else: leg_top, leg_bottom = None, self.n_rows - leg_span menu_layout = [] sel_opts_span = self._get_span(select_opts.ax) sel_opts_top, sel_opts_bottom = leg_bottom - 1, leg_bottom - sel_opts_span - 1 menu_layout += [self.cell_height] * sel_opts_span + [pad] vx_opts_span = self._get_span(self._vertex_opts.ax) vx_opts_top, vx_opts_bottom = sel_opts_bottom - 1, sel_opts_bottom - vx_opts_span - 1 menu_layout += [self.cell_height] * vx_opts_span + [pad] # Placeholder for unused space menu_layout += [self.cell_height] * (vx_opts_bottom - 1) divider.set_vertical([c for c in reversed(menu_layout)]) self.divider = divider graph.ax.set_axes_locator(divider.new_locator(nx=0, ny=0, ny1=-1)) ax.figure.add_axes(graph.ax) self._legend.ax.set_axes_locator( divider.new_locator(nx=2, ny=leg_bottom, ny1=leg_top)) ax.figure.add_axes(self._legend.ax) select_opts.ax.set_axes_locator( divider.new_locator(nx=2, ny=sel_opts_bottom, ny1=sel_opts_top)) ax.figure.add_axes(select_opts.ax) self._vertex_opts.ax.set_axes_locator( divider.new_locator(nx=2, ny=vx_opts_bottom, ny1=vx_opts_top)) ax.figure.add_axes(self._vertex_opts.ax) ax.set_axes_locator( divider.new_locator(nx=2, ny=0, ny1=vx_opts_bottom - 1)) ax.tick_params(left=False, labelleft=False, bottom=False, labelbottom=False)