Plotting (s2stools.plot)

Some useful plotting routines, especially because leadtime is of type timedelta64[ns] which annoyingly is displayed in nanoseconds.

Functions

s2stools.plot.add_box(dict_lon_lat_slice, ax, **kwargs)

Add a box to a map projection plot.

Parameters:
  • dict_lon_lat_slice (dict) – E.g. {“longitude”: slice(0, 180), “latitude”: slice(40, 60)}

  • ax (axis) – matplotlib axis

  • kwargs (dict) – additional keyword arguments to format/ overwrite appearance, e.g.: facecolor, edgecolor, lw, zorder

Warning

Requires cartopy (which is not a formal dependency of s2stools.

s2stools.plot.add_map(ax, **kwargs)

Add a nicely formatted map to a proplot axis. Works only with proplot https://proplot.readthedocs.io/en/stable/.

Parameters:
  • ax (proplot axis)

  • kwargs (dict) – parameters passed to ax.format(…). overwrites default parameters.

s2stools.plot.add_map_inset(ax, loc='ul', size=0.3, proj='cyl', lonlim=(-180, 180), latlim=(0, 90), inset_kw={}, draw_box=None, box_kw={}, format_kw={})

Add a small map to an existing axis. Designed to indicate geographic location of some other analysis.

Parameters:
  • ax (proplot.axes.Axes) – axis to which map should be added

  • loc (str) – location of map, must be one of “ul”, “ur”, “ll”, “lr”

  • size (float) – size of map in relative units of axis object, defaults to 0.3.

  • proj (str) – map projection

  • lonlim (2-tuple of float) – longitude limits for map

  • latlim (2-tuple of float) – latitude limits for map

  • inset_kw (dict) – Addictional keyword arguments passed to ax.inset()

  • draw_box (dict) – Dictionary of form {‘longitude’: slice(lon1, lon2), ‘latitude’: slice(lat1, lat2)} corresponding to location of box. Defaults to None, in which case no box is drawn.

  • box_kw (dict) – Addictional keyword arguments passed to s2stools.plot.add_box()

  • format_kw (dict) – Addictional keyword arguments passed to ax.format()

Return type:

proplot axis of inset

s2stools.plot.cmap_spread(less_dark_blue=0, less_dark_red=0)

Colormap that I use often for ensemble variance anomalies.

Parameters:
  • less_dark_blue (float) – If the blue is too dark, setting this parameter to >0 but <.5 results in less dark blue.

  • less_dark_red (float) – If the red is too dark, setting this parameter to >0 but <.5 results in less dark red.

Return type:

pplt.Colormap

s2stools.plot.cyclic_xyz(field, longitude_name='longitude', latitude_name='latitude')

Add cyclic point to longitude such that vertical white line gets removed on contour plots. Use as example below.

Parameters:
  • field (xr.DataArray)

  • longitude_name (str) – Defaults to ‘longitude’

  • latitude_name (str) – Defaults to ‘latitude’

Returns:

x, y, z DataArrays

Return type:

tuple

Examples

>>> fig, ax = plt.subplots()
>>> ax.contourf(*dataarray, cmap='coolwarm')

Warning

Don’t forget the star expression for unpacking.

Warning

Requires cartopy (which is not a formal dependency of s2stools.

s2stools.plot.fill_between(dataarray, x, y, ax=None, ci=None, **kwargs)

wraps matplotlib’s fill_between for DataArrays

Parameters:
  • dataarray (xr.DataArray)

  • x (str) – coordinate name

  • y (str) – coordinate name

  • ci ((tuple)) – quantiles

  • ax (plt.axis) – axis to draw plot, if None call plt.fill_between()

  • kwargs (dict) – kwargs passed to matplotlib’s fill_between

Return type:

void

s2stools.plot.legend_colored_labels_no_lines(ax, fontweight='bold', **kwargs)

Similar to ax.legend(), but instead of drawing lines and putting labels next to them, just write the text in the right color.

Parameters:
  • ax (matplotlib axis or proplot axis)

  • fontweight (str) – Defaults to “bold”.

  • kwargs (dict) – additional keyword arguments passed to ax.legend()

Return type:

legend

s2stools.plot.plot_dots_where(dataarray, condition, every_nth_lon=1, every_nth_lat=1, **scatter_kws)

Scatter Points where condition is fulfilled.

Parameters:
  • dataarray (xr.DataArray) – Data Field which is used for condition.

  • condition (lambda expression) – For example lambda x: x > 0 would scatter points where dataarray is positive.

  • every_nth_lon (int) – Skip every every_nth_lon th longitude point when scatter plotting.

  • every_nth_lat (int) – Skip every every_nth_lat th latitude point when scatter plotting.

  • scatter_kws (dict) – kwargs passed to matplotlib’s scatter

s2stools.plot.set_xticks_with_colored_labels(ax, xticks, xticklabels, colors, fontweight='bold')

Set x-axis ticks with colored labels.

Parameters:
  • ax (axis) – The axis object

  • xticks (list)

  • xticklabels (list)

  • colors (list)

  • fontweight (str) – Defaults to “bold”

Return type:

None

s2stools.plot.set_yticks_with_colored_labels(ax, yticks, yticklabels, colors, fontweight='bold')

Set x-axis ticks with colored labels.

Parameters:
  • ax (axis) – The axis object

  • yticks (list)

  • yticklabels (list)

  • colors (list)

  • fontweight (str) – Defaults to “bold”

Return type:

None

s2stools.plot.spaghetti(**kwargs)

Some standard keyword parameters to produce a ensemble forecast spaghetti plot. Standard format is dict(c="k", add_legend=False, alpha=0.5, lw=1.5, hue="number").

Parameters:

kwargs (mappable) – keyword arguments (optional)

Returns:

Spaghetti keyword dictionary

Return type:

dict

s2stools.plot.symmetric_ylim(ax)

For example, change y limits from -3, 1 to -3, 3. It will be symmetric around 0.

Parameters:

ax (plt.Axis) – Axis.

s2stools.plot.xaxis_unit_days(ax=None, multiple=7, minor_multiple=1)

Convert nanoseconds on x-axis to days.

Parameters:
  • ax (plt.Axis)

  • multiple (int) – Defaults to every 7 days.

  • minor_multiple (int) – Defaults to every 1 day.

s2stools.plot.xlim_days(ax=None, leftlim=None, rightlim=None)

Adjust limits.

Parameters:
  • ax (plt.Axis) – apply limit to a given axis

  • leftlim (float) – day left limit

  • rightlim (float) – day right limit

Return type:

(leftlim, rightlim)