rcatool.plot package
Submodules
rcatool.plot.colors module
This module provides access to predefined (Matplotlib) or self-produced colors and color maps.
@author Petter Lind @date 2015-05-18
- rcatool.plot.colors.getcolormap(cmap_name, custom=False)[source]
Function to retrieve colormap, either customized (custom=True) or available through Matplotlib predefined colormaps.
- Parameters:
cmap_name (string) – String, giving name of colormap to be retrieved.
custom (Boolean) – Logical indicating self-produced (custom=True) or Matplotlib colormap.
- Returns:
cmap
- Return type:
Matplotlib colormap object
rcatool.plot.plots module
Graphics module
Functions for different plots such as scatterplots and mapplots, initiation and configuration of figure objects etc
Created: Autumn 2016 Authors: Petter Lind & David Lindstedt
- rcatool.plot.plots.axes_settings(ax, figtitle=None, xlabel=None, ylabel=None, xtlabels=None, ytlabels=None, xticks=None, yticks=None, xlim=None, ylim=None, color='k', fontsize='xx-large', fontsize_lbls='xx-large', fontsize_title='xx-large', ftitle_location='center')[source]
Configuration of axes; titles and labels
- Parameters:
ax (axes object) – Axes object assoicated with figure
figtitle (string) – The headtitle
xlabel (strings) – Labels of the axes
ylabel (strings) – Labels of the axes
xticks (lists or arrays) – If set, location of ticks
yticks (lists or arrays) – If set, location of ticks
xtlabels (lists) – Contains tick labels (corresponding to ticks location)
ytlabels (lists) – Contains tick labels (corresponding to ticks location)
xlim (lists) – Limits of the axes
ylim (lists) – Limits of the axes
color (str) – Color for the xtick labels
fontsize/fontsize_lbls/fontsize_title (string or int) – Size of font for axes labels, ticklabels, and figure title respectively
ftitle_location (str) – Horizontal alignment of figure title; center (default), right or left.
- rcatool.plot.plots.custom_legend(colors, labels, linestyles=None)[source]
Creates a list of matplotlib Patch objects that can be passed to the legend(…) function to create a custom legend.
- Parameters:
colors (list) – A list of colors, one for each entry in the legend. You can also include a linestyle, for example: ‘k–’
labels (list) – A list of labels, one for each entry in the legend.
- rcatool.plot.plots.define_map_object(projection='Stereographic', **proj_kwargs)[source]
Create a Cartopy map object
Available map projections and configurations: https://scitools.org.uk/cartopy/docs/v0.15/crs/projections.html
- rcatool.plot.plots.fig_grid_setup(figsize=(12, 12), fshape=(1, 1), direction='row', axes_pad=(None, None), **grid_kwargs)[source]
Set up the plot axes using pyplot.subplots
- Parameters:
figsize (tuple) – Size of figure in inches; (width, height)
fshape (tuple) – setting the shape of figure (nrow, ncol)
direction (string) – ‘row’ or ‘col’; rowwise or columnwise order of axes instances
axes_pad (tuple) – padding (height,width) between edges of adjacent subplots
**grid_kwargs (Additional keyword arguments)
- Returns:
fig (Figure object)
grid (List with axes instances)
- rcatool.plot.plots.figure_init(plottype='line', printtypes=False)[source]
Setting up a figure object
- Parameters:
plottype (string) – Type of plot to make
printtypes (boolean) – If available plottypes should be printed on screen
- rcatool.plot.plots.gen_clevels(data, nsteps, robust=None)[source]
Create contour levels based on min and max of input data
- Parameters:
data (array) – data array
nsteps (int) – number of levels to be produced
robust (None or string) – If to soften the max/min limits due to extreme values; “top”/”bottom”/”both”, which end(s) to soften.
- rcatool.plot.plots.get_nrow_ncol(npanels)[source]
Return number of rows and columns from a given total number of panels for a grid
- rcatool.plot.plots.image_colorbar(cs, cbaxs, title=None, labelspacing=1, labelsize='x-large', formatter='{:.2f}', **cbar_kwargs)[source]
Add colobar to map plot
- Parameters:
cs (Plot object) – Such as an image (imshow) or a contour set (with contourf)
cbaxs (cbar axis object/list) – Colorbar axis object or list with axes objects for each plot in figure
title (list) – list of strings with colorbar titles
labelspacing (int) – Label spacing; the integer value represents the number of steps between each label. 1 show each label (default), 2 every second, etc.
labelsize (str/int) – Size of labels; integer value or a string (e.g. ‘large’)
**cbar_kwargs (keyword arguments) – arguments (key=value) that can be used in pyplot.colobar See http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.colorbar
- rcatool.plot.plots.image_grid_setup(figsize=(12, 12), fshape=(1, 1), **grid_kwargs)[source]
Set up the plot axes using mpl_toolkits.axes_grid1.ImageGrid Used primarily when plotting maps or for image analysis For more information on available settings: https://doc.ebichu.cc/matplotlib/mpl_toolkits/axes_grid1/overview.html
- Parameters:
figsize (tuple) – Size of figure in inches; (width, height)
fshape (tuple) – setting the shape of figure (nrow, ncol)
**grid_kwargs (Additional keyword arguments)
- Returns:
fig (Figure object)
grid (AxesGrid object)
- Examples of
**kwargswith default values: direction=”row”, axes_pad=0.02, add_all=True, share_all=False, label_mode=”L”, aspect=True, cbar_mode=None, cbar_location=”right”, cbar_size=”5%”, cbar_pad=None,
- rcatool.plot.plots.make_box_plot(grid, data, labels=None, leg_labels=None, grouped=False, box_colors=None, **box_kwargs)[source]
Create a box plot
- Parameters:
grid (AxesGrid object) – returned from the ‘fig_grid_setup’ function
data (List/Array) – 1d array or list of 1d arrays with data for boxplot
labels (str/list) – String or a list of strings with xtick labels (for each box/group of boxes)
leg_labels (str/list) – String or a list of strings with legend labels (mostly used for grouped boxplots).
grouped (boolean) – Whether to plot grouped boxplot. If True, input data must be a dictionary. See _grouped_boxplot function for more info.
box_colors (array/list) – Optional list of colors to be used for the boxes.
**box_kwargs (keyword arguments) – arguments (key=value) that can be used in pyplot.boxplot See matplotlib.org for more information
- Returns:
axs (list) – Axes objects for each plot
bps (list) – Each item in list is a dictionary mapping each component of the boxplot to a list of the .Line2D instances created.
- rcatool.plot.plots.make_line_plot(grid, ydata, xdata=None, labels=None, lbl_fontsize='x-large', axis_type='linear', **lp_kwargs)[source]
Create a line plot
- Parameters:
grid (Axis object) – returned from the ‘fig_grid_setup’ function
ydata (array/list) – Required. 1D array or list of 1D arrays with data for y axis
xdata (array/list) – Optional. 1D array or list of 1D arrays with data for x axis
labels (string/list) – String or list of strings with line labels
lbl_fontsize (string/float) – Fontsize for legend labels
axis_type (str) – Linear or log axes: ‘linear’ (defualt), ‘logx’/’logy’/’logxy’ (log x, y or both axes).
**lp_kwargs (keyword arguments) – arguments (key=value) that can be used in pyplot.line See matplotlib.org for more information
- Returns:
axs – The axes objects created for each plot
- Return type:
Axes objects
- rcatool.plot.plots.make_map_plot(data, grid, lats, lons, mesh=False, filled=True, cmap=None, clevs=None, robust=None, norm=None, **map_kwargs)[source]
Producing map plots
- Parameters:
data (array/list/tuple) – Array or list/tuple of 2D data array(s) to plot
grid (AxesGrid object) – Returned from the ‘map_setup’ function
lats (arrays/list of arrays) – Values of latitudes and longitudes needed for the map plotting. If different lats/lons should be used in the figure panels, these arrays should be provided in a list; [lons_1, lons_2, …].
lons (arrays/list of arrays) – Values of latitudes and longitudes needed for the map plotting. If different lats/lons should be used in the figure panels, these arrays should be provided in a list; [lons_1, lons_2, …].
mesh (boolean) – Whether to plot data as mesh. If false (default), contour plot is made.
filled (boolean) – Whether to color fill between contours or not. Defaults to True
cmap (string/list) – String or list with strings of predefined Matplotlib colormaps. For filled contour plots it defaults to ‘viridis’.
clevs (Iterable data structure) – Consisting of lists with defined contour levels; e.g. (np.arange(1,10,2), [0,2,4,6,8]), [np.arange(100,step=5)]*3
robust (string) – See gen_clevs function for info
norm (BoundaryNorm object) – Object generated from matplotlib.colors.BoundaryNorm function. Generate a colormap index based on discrete intervals.
**map_kwargs (keyword arguments) – arguments (key=value) that can be used in pyplot.contour/f (if mesh=False) or pcolormesh (if mesh=True)
- Returns:
mplots – List with map plot instances
- Return type:
List
- rcatool.plot.plots.make_raster_plot(data, grid=None, clevs=None, norm=None, cmap='viridis', **rs_kwargs)[source]
Create a raster plot
- Parameters:
grid (AxesGrid object) – returned from the ‘image_grid_setup’ function
data (List) – List with 2D array(s) of data
cmap (string/list) – String or list with strings of predefined Matplotlib colormaps. Defaults to ‘viridis’
clevs (Iterable data structure) – Consisting of lists with defined contour levels; e.g. (np.arange(1,10,2), [0,2,4,6,8]), [np.arange(100,step=5)]*3
norm (BoundaryNorm object) – Object generated from matplotlib.colors.BoundaryNorm function. Generate a colormap index based on discrete intervals.
**rs_kwargs (keyword arguments) – arguments (key=value) that can be used in pyplot.imshow See matplotlib.org for more information
- Returns:
axs (Axes objects) – The axes objects created for each plot
rasters (Plot objects) – The raster plot objects created for each plot
- rcatool.plot.plots.make_scatter_plot(grid, xdata, ydata, sdata=None, fcolors=None, ecolors=None, lbl_fontsize='large', axis_type='linear', labels=None, **sc_kwargs)[source]
Create a scatter plot
- Parameters:
grid (AxesGrid object) – returned from the ‘fig_grid_setup’ function
xdata/ydata (Array/list) – 1D array or list of 1D arrays with data for x/y axis
fcolors (array/list) – List of colors to be used for each data set in input data. This is separate from ‘color’/’c’ option available from matplotlib.scatter call (and set in sc_kwargs) where all individual input data sets will have that specific color/s. If set, then ecolors need also be supplied.
ecolors (array/list) – List of edge colors to be used for each data set in input data. This is separate from ‘edgecolors’/’ec’ option available from matplotlib.scatter call (and set in sc_kwargs) where all individual input data sets will have that specific color/s. If set, then fcolors need also be supplied.
lbl_fontsize (string/float) – Fontsize for legend labels
axis_type (str) – Linear or log axes: ‘linear’ (defualt), ‘logx’/’logy’/’logxy’ (log x, y or both axes).
labels (String/List) – String or list of strings with legend labels
**sc_kwargs (keyword arguments) – arguments (key=value) that can be used in pyplot.scatter See matplotlib.org for more information
- Returns:
axs – The axes objects created for each plot
- Return type:
Axes objects
- rcatool.plot.plots.map_axes_settings(fig, axs, figtitle=None, headtitle=None, time_mean=None, time_units=None, fontsize='x-large', fontsize_htitle='xx-large')[source]
Settings for map plot axes
- Parameters:
fig (Object handle) – Figure handle
axs (Axes objects) – Single object or list with axes from map plot
figtitle (Strings) – Single title or list of titles for each plot in figure
headtitle (String) – Head title of figure
time_mean (string) – If maps should be labeled according to time averages; ‘season’/’month’/’hour’
time_units (list/array) – Optional. If time_mean is set, time_units is a list of seasons, months or hours to be used in the labeling.
fontsize (string or int) – Size of font for figure title
fontsize_htitle (string or int) – Size of font for suptitle
- rcatool.plot.plots.map_setup(map_proj, map_extent, figsize=(12, 12), figshape=(1, 1), grid_lines=False, **grid_kwargs)[source]
Create a Cartopy crs object to be used in the overlaying of 2d plots.
- Parameters:
map_proj (Cartopy map projection) – Generated from the ‘define_map_object’ function
map_extent (list/tuple) – Longitude/latitude values for map extent; [lon_start, lon_end, lat_start, lat_end]
figsize (tuple) – The requested size of figure object
figshape (tuple) – The requested shape (rows, columns) of figure panels
grid_lines (boolean) – Whether to plot grid lines (lat/lon) with values along the left and bottom axes. Default False.
**grid_kwargs (key word arguments) – Additional arguments provided to AxesGrid (See below for more info).
- Returns:
fig – figure object
axgrid (list) – List with axes objects, with GeoAxes class, for each plot in figure
With AxesGrid and GeoAxes, a grid is created and corresponding axes are classified with GeoAxes. Read more about AxesGrid, and the available options, here: https://matplotlib.org/2.0.2/mpl_toolkits/axes_grid/users/overview.html
- Examples of
**kwargswith default values: direction=”row”, axes_pad=0.02, add_all=True, share_all=False, label_mode=”L”, aspect=True, cbar_mode=None, cbar_location=”right”, cbar_size=”5%”, cbar_pad=None,
- rcatool.plot.plots.plot_map(ax, x, y, data, clevs, cmap, norm, mesh, filled, **map_kwargs)[source]
Producing a map plot
- Parameters:
ax (Axis object) – Axis generated in ‘map_setup’ function
data (numpy array) – 2D data array to plot
x (numpy arrays) – Arrays of lat/lon coordinates
y (numpy arrays) – Arrays of lat/lon coordinates
clevs (List/array) – Contour levels
cmap (string) – Color map. See http://matplotlib.org/users/colormaps.html for more information.
norm (Boundary norm object) – Normalize data to [0,1] to use for mapping colors
mesh (boolean) – Whether to plot data as mesh. If false (default), contour plot is made.
filled (Boolean) – Whether to color fill between contours or not. Defaults to True
**map_kwargs (keyword arguments) – arguments (key=value) that can be used in pyplot.contour/f (if mesh=False) or pcolormesh (if mesh=True)
- Returns:
cs
- Return type:
Contour plot object