lair.utils.plotter#

This module provides utility functions for plotting data.

Functions

NCL_cmap(table_name)

Generate matplotlib colormap from NCL color table.

create_polar_ax()

Create a polar axis with North at the top.

diurnalPlot(data, param[, stats, units, tz, ...])

Plot the diurnal cycle of data.

format_radial_axis(ax, x, scale_angle)

Format radial axis of polar plot.

log10formatter(x, pos[, deci])

Format ticks to log 10 format with deci number of decimals.

polarFreq(data[, x, wd, xbins, scale_angle])

Plot the contoured frequency of the data.

polarPlot(data[, param, x, wd, statistic, ...])

Plot polar contour of data.

seasonalPlot(data[, param, units, ax])

Plot the seasonal cycle of data by year.

terrain_cmap([minval, maxval, n])

Matplotlib terrain cmap.

truncate_colormap(cmap[, minval, maxval, n])

Truncate matplotlib colormaps using min and max vals from 0 to 1, and then linearly build a new colormap

windvectorPlot(data[, wd, ws, ax, unit_length])

Plot wind vectors.

Classes

HandlerDashedLines([marker_pad, numpoints])

Custom Handler for LineCollection instances.

lair.utils.plotter.log10formatter(x, pos, deci=0) str[source]#

Format ticks to log 10 format with deci number of decimals.

Parameters:
xfloat

Tick value.

deciint, optional

Number of decimals to display. Defaults to 0.

Returns:
str

Formatted tick label.

Examples

>>> from functools import partial
>>> import numpy as np
>>> data: xr.DataArray  # some data, in this case, 3D (time, lat, lon)
>>> np.log10(data).plot(cbar_kwargs={'format': partial(log10formatter, deci=2)})
lair.utils.plotter.truncate_colormap(cmap: str | Colormap, minval: float = 0.0, maxval: float = 1.0, n: int = 100) LinearSegmentedColormap[source]#

Truncate matplotlib colormaps using min and max vals from 0 to 1, and then linearly build a new colormap

Parameters:
cmapstr | matplotlib.colors.Colormap

Colormap to be truncated.

minvalfloat, optional

Minimum value to truncate the colormap. Defaults to 0.0.

maxvalfloat, optional

Maximum value to truncate the colormap. Defaults to 1.0.

nint, optional

Number of colors in the new colormap. Defaults to 100.

Returns:
matplotlib.colors.LinearSegmentedColormap

Truncated colormap.

lair.utils.plotter.NCL_cmap(table_name: str) LinearSegmentedColormap[source]#

Generate matplotlib colormap from NCL color table.

Parameters:
table_namestr
Returns:
matplotlib.colors.LinearSegmentedColormap

matplotlib colormap from NCL color table.

lair.utils.plotter.terrain_cmap(minval: float = 0.42, maxval: float = 1.0, n: int = 256) LinearSegmentedColormap[source]#

Matplotlib terrain cmap.

Parameters:
minvalfloat, optional

Minimum value to truncate the colormap. Defaults to 0.42.

maxvalfloat, optional

Maximum value to truncate the colormap. Defaults to 1.0.

nint, optional

Number of colors in the new colormap. Defaults to 256.

Returns:
matplotlib.colors.LinearSegmentedColormap

Matplotlib terrain colormap.

lair.utils.plotter.diurnalPlot(data: DataFrame, param: str, stats: str | list[str] = ['std', 'median', 'mean'], units: str | None = None, tz: str = 'UTC', freq: str = '1H', ax: Axes | None = None, colors: str | dict[str, str] = {'mean': 'black', 'median': 'blue', 'std': 'gray'}, min_count: int = 0) Axes[source]#

Plot the diurnal cycle of data.

Parameters:
datapd.DataFrame

Data to plot.

paramstr

Parameter to plot.

statsstr | list[str], optional

Statistics to plot. Defaults to [‘std’, ‘median’, ‘mean’].

unitsstr | None, optional

Units of the parameter for the ylabel. Defaults to None.

tzstr, optional

Timezone of the data. Defaults to ‘UTC’.

Warning

DOES NOT CONVERT TIMEZONES.

freqstr, optional

Frequency of the data. Defaults to ‘1H’.

axplt.Axes | None, optional

Axis to plot on. Defaults to None.

colorsstr | dict[str, str], optional

Colors of the statistics. Defaults to {‘mean’: ‘black’, ‘median’: ‘blue’, ‘std’: ‘gray’}.

min_countint, optional

Minimum count to plot. Defaults to 0.

Returns:
plt.Axes

Axis with the plot

lair.utils.plotter.seasonalPlot(data: DataFrame, param: str = 'CH4', units: str = 'ppm', ax: Axes | None = None) Axes[source]#

Plot the seasonal cycle of data by year.

Parameters:
datapd.DataFrame

Data to plot.

paramstr, optional

Parameter to plot. Defaults to ‘CH4’.

unitsstr, optional

Units of the parameter for the ylabel. Defaults to ‘ppm’.

axplt.Axes | None, optional

Axis to plot on. Defaults to None.

Returns:
plt.Axes

Axis with the plot

lair.utils.plotter.create_polar_ax() Axes[source]#

Create a polar axis with North at the top.

Returns:
plt.Axes

Polar axis.

lair.utils.plotter.format_radial_axis(ax: Axes, x: str, scale_angle: float) None[source]#

Format radial axis of polar plot.

Parameters:
axplt.Axes

Axis to format.

xstr

Label of the radial axis.

scale_anglefloat

Angle to position the label.

Returns:
None
lair.utils.plotter.polarPlot(data: DataFrame, param: str = 'CH4', x: str = 'ws', wd: str = 'wd', statistic: str = 'mean', units: str = 'ppm', min_bin: int = 1, xbins: int = 30, scale_angle: float | None = None) Axes[source]#

Plot polar contour of data.

Parameters:
datapd.DataFrame

Data to plot.

paramstr, optional

Parameter to plot. Defaults to ‘CH4’.

xstr, optional

Variable to bin in the radial axis. Defaults to ‘ws’.

wdstr, optional

Variable to bin in the angular axis. Defaults to ‘wd’.

statisticstr, optional

Statistic to plot. Defaults to ‘mean’.

unitsstr, optional

Units of the parameter for the colorbar label. Defaults to ‘ppm’.

min_binint, optional

Minimum count in each bin to plot. Defaults to 1.

xbinsint, optional

Number of bins in the radial axis. Defaults to 30.

scale_anglefloat | None, optional

Angle to position the radial axis label. Defaults to None.

Returns:
plt.Axes

Axis with the plot

lair.utils.plotter.polarFreq(data: DataFrame, x: str = 'ws', wd: str = 'wd', xbins: int = 30, scale_angle: float | None = None) Axes[source]#

Plot the contoured frequency of the data.

Parameters:
datapd.DataFrame

Data to plot.

xstr, optional

Variable to bin in the radial axis. Defaults to ‘ws’.

wdstr, optional

Variable to bin in the angular axis. Defaults to ‘wd’.

xbinsint, optional

Number of bins in the radial axis. Defaults to 30.

scale_anglefloat | None, optional

Angle to position the radial axis label. Defaults to None.

Returns:
plt.Axes

Axis with the plot

lair.utils.plotter.windvectorPlot(data: DataFrame, wd: str = 'WD', ws: str = 'WS', ax: Axes | None = None, unit_length: bool = False, **kwargs) Axes[source]#

Plot wind vectors.

Parameters:
datapd.DataFrame

Data to plot.

wdstr, optional

Wind direction variable. Defaults to ‘WD’.

wsstr, optional

Wind speed variable. Defaults to ‘WS’.

axplt.Axes | None, optional

Axis to plot on. Defaults to None.

unit_lengthbool, optional

Normalize vectors to unit length. Defaults to False.

**kwargs

Additional arguments to pass to plt.quiver.

Returns:
plt.Axes

Axis with the plot

class lair.utils.plotter.HandlerDashedLines(marker_pad=0.3, numpoints=None, **kwargs)[source]#

Custom Handler for LineCollection instances.

https://stackoverflow.com/questions/31544489/two-line-styles-in-legend

There’s a potentially better version in the above link

This needs a new names and better documentation.

create_artists(legend, orig_handle, xdescent, ydescent, width, height, fontsize, trans)[source]#

Return the legend artists generated.

Parameters:
legend~matplotlib.legend.Legend

The legend for which these legend artists are being created.

orig_handle~matplotlib.artist.Artist or similar

The object for which these legend artists are being created.

xdescent, ydescent, width, heightint

The rectangle (xdescent, ydescent, width, height) that the legend artists being created should fit within.

fontsizeint

The fontsize in pixels. The legend artists being created should be scaled according to the given fontsize.

trans~matplotlib.transforms.Transform

The transform that is applied to the legend artists being created. Typically from unit coordinates in the handler box to screen coordinates.