Source code for panoptes.utils.images.plot

from copy import copy

import numpy as np
from matplotlib import cm
from mpl_toolkits.axes_grid1 import make_axes_locatable


[docs]def get_palette(cmap='inferno'): """Get a palette for drawing. Returns a copy of the colormap palette with bad pixels marked. Args: cmap (str, optional): Colormap to use, default 'inferno'. Returns: `matplotlib.cm`: The colormap. """ palette = copy(getattr(cm, cmap)) # Mark bad pixels (e.g. saturated) # when using vmin or vmax and a normalizer. palette.set_over('w', 1.0) palette.set_under('k', 1.0) palette.set_bad('g', 1.0) return palette
[docs]def add_colorbar(axes_image, size='5%', pad=0.05, orientation='vertical'): """Add a colorbar to the image. This is a simple convenience function to add a colorbar to a plot generated by `matplotlib.pyplot.imshow`. .. plot:: :include-source: :caption: A colorbar with sane settings. >>> from matplotlib import pyplot as plt >>> import numpy as np >>> from panoptes.utils.images.plot import add_colorbar >>> >>> x = np.arange(0.0, 100.0) >>> y = np.arange(0.0, 100.0) >>> X, Y = np.meshgrid(x, y) >>> >>> func = lambda x, y: x**2 + y**2 >>> z = func(X, Y) >>> >>> fig, ax = plt.subplots() >>> im1 = ax.imshow(z, origin='lower') >>> >>> # Add the colorbar to the Image object (not the Axes). >>> add_colorbar(im1) >>> >>> fig.show() Args: axes_image (`matplotlib.image.AxesImage`): A matplotlib AxesImage. """ divider = make_axes_locatable(axes_image.axes) cax = divider.append_axes('right', size=size, pad=pad) axes_image.figure.colorbar(axes_image, cax=cax, orientation=orientation)
[docs]def add_pixel_grid(ax1, grid_height, grid_width, show_axis_labels=True, show_superpixel=False, major_alpha=0.5, minor_alpha=0.25): """ Adds a pixel grid to a plot, including features for the Bayer array superpixel. .. plot:: :include-source: :caption: The Bayer array superpixel pattern. Grid height and size must be even. >>> from matplotlib import pyplot as plt >>> import numpy as np >>> from panoptes.utils.images.plot import add_pixel_grid >>> >>> x = np.arange(-5, 5) >>> y = np.arange(-5, 5) >>> X, Y = np.meshgrid(x, y) >>> func = lambda x, y: x**2 - y**2 >>> >>> fig, ax = plt.subplots() >>> im1 = ax.imshow(func(X, Y), origin='lower', cmap='Greys') >>> >>> # Add the grid to the Axes object. >>> add_pixel_grid(ax, grid_height=10, grid_width=10, show_superpixel=True, show_axis_labels=False) >>> >>> fig.show() Args: ax1 (`matplotlib.axes.Axes`): The axes to add the grid to. grid_height (int): The height of the grid in pixels. grid_width (int): The width of the grid in pixels. show_axis_labels (bool, optional): Whether to show the axis labels. Default True. show_superpixel (bool, optional): Whether to show the superpixel pattern. Default False. major_alpha (float, optional): The alpha value for the major grid lines. Default 0.5. minor_alpha (float, optional): The alpha value for the minor grid lines. Default 0.25. """ ax1.set_xticks([]) ax1.set_yticks([]) # major ticks every 2, minor ticks every 1 if show_superpixel: x_major_ticks = np.arange(-0.5, grid_width, 2) y_major_ticks = np.arange(-0.5, grid_height, 2) ax1.set_xticks(x_major_ticks) ax1.set_yticks(y_major_ticks) ax1.grid(which='major', color='r', linestyle='--', lw=3, alpha=major_alpha) x_minor_ticks = np.arange(-0.5, grid_width, 1) y_minor_ticks = np.arange(-0.5, grid_height, 1) ax1.set_xticks(x_minor_ticks, minor=True) ax1.set_yticks(y_minor_ticks, minor=True) ax1.grid(which='minor', color='r', lw='2', linestyle='--', alpha=minor_alpha) if show_axis_labels is False: ax1.set_xticklabels([]) ax1.set_yticklabels([])