time_series_plots
Figure Module¶Development of functions for nowcast.figures.research.time_series_plots
web site figure module.
from types import SimpleNamespace
import matplotlib.pyplot as plt
import xarray as xr
import arrow as arw
from salishsea_tools import places
import nowcast.figures.website_theme
from importlib import reload
%matplotlib inline
What make_plots provides:
place = 'Sandheads'
left_variable = 'nitrate'
right_variable = 'diatoms'
xr_dataset = xr.open_dataset('https://salishsea.eos.ubc.ca/erddap/griddap/ubcSSg3DBiologyFields1hV17-02')
xr_dataset
<xarray.Dataset> Dimensions: (depth: 40, gridX: 398, gridY: 898, time: 9096) Coordinates: * time (time) datetime64[ns] 2014-09-12T00:30:00 ... * depth (depth) float32 0.5 1.5 2.50001 3.50003 ... * gridY (gridY) int16 0 1 2 3 4 5 6 7 8 9 10 11 12 ... * gridX (gridX) int16 0 1 2 3 4 5 6 7 8 9 10 11 12 ... Data variables: Fraser_tracer (time, depth, gridY, gridX) float64 ... ammonium (time, depth, gridY, gridX) float64 ... biogenic_silicon (time, depth, gridY, gridX) float64 ... ciliates (time, depth, gridY, gridX) float64 ... diatoms (time, depth, gridY, gridX) float64 ... dissolved_organic_nitrogen (time, depth, gridY, gridX) float64 ... flagellates (time, depth, gridY, gridX) float64 ... mesozooplankton (time, depth, gridY, gridX) float64 ... microzooplankton (time, depth, gridY, gridX) float64 ... nitrate (time, depth, gridY, gridX) float64 ... particulate_organic_nitrogen (time, depth, gridY, gridX) float64 ... silicon (time, depth, gridY, gridX) float64 ... Attributes: acknowledgement: MEOPAR, ONC, Compute Canada cdm_data_type: Grid Conventions: CF-1.6, COARDS, ACDD-1.3 creator_email: sallen@eoas.ubc.ca creator_name: SalishSeaCast Project Contributors creator_url: https://salishsea-meopar-docs.readthedocs.io/ description: biogeochemical variables drawLandMask: over history: 2017-06-18T21:27:08Z (local files)\n2017-06-18... infoUrl: https://salishsea-meopar-docs.readthedocs.io/e... institution: UBC EOAS institution_fullname: Earth, Ocean & Atmospheric Sciences, Universit... keywords: ammonia, ammonium, aquatic, biogenic, biogenic... keywords_vocabulary: GCMD Science Keywords license: The SalishSeaCast NEMO model results are c... project: SalishSeaCast NEMO Model sourceUrl: (local files) standard_name_vocabulary: CF Standard Name Table v29 summary: Green, Salish Sea, 3d Biology Fields, Hourly, ... time_coverage_end: 2017-05-27T23:30:00Z time_coverage_start: 2014-09-12T00:30:00Z timeStamp: 2017-Jun-01 20:53:05 GMT title: Green, Salish Sea, 3d Biology Fields, Hourly, ... uuid: 6a76eb14-b8b4-47b0-b3e0-b695ad2355fd
_prep_plot_data()
Function¶def _prep_plot_data(xr_dataset, left_variable, right_variable, place):
end_day = arw.get(xr_dataset.time_coverage_end)
start_day = end_day.replace(days=-1)
time_slice = slice(start_day.date(), end_day.replace(days=+1).date())
gridY, gridX = places.PLACES[place]['NEMO grid ji']
left = xr_dataset[left_variable].sel(time=time_slice).isel(depth=0, gridX=gridX, gridY=gridY)
right = xr_dataset[right_variable].sel(time=time_slice).isel(depth=0, gridX=gridX, gridY=gridY)
left_long_name = xr_dataset[left_variable].long_name
left_units = xr_dataset[left_variable].units
right_long_name = xr_dataset[right_variable].long_name
right_units = xr_dataset[right_variable].units
return SimpleNamespace(left=left, right=right, left_long_name=left_long_name, left_units=left_units,
right_long_name=right_long_name, right_units=right_units)
_prep_fig_axes() Function
¶def _prep_fig_axes(figsize, theme):
fig, axl = plt.subplots(
figsize=figsize, facecolor=theme.COLOURS['figure']['facecolor'])
axl.set_axis_bgcolor(theme.COLOURS['axes']['background'])
axr = axl.twinx()
axr.set_axis_bgcolor(theme.COLOURS['axes']['background'])
return fig, axl, axr
_plot_timeseries()
Function¶def _plot_timeseries(ax, plot_data, variable, theme):
ax.plot(plot_data.time, plot_data, color=theme.COLOURS['time series'][variable])
return
_timeseries_axes_labels()
Function¶def _timeseries_axes_labels(axl, axr, left_variable, left_long_name, left_units,
right_variable, right_long_name, right_units, theme):
axl.set_xlabel(
'Date',
color=theme.COLOURS['text']['axis'],
fontproperties=theme.FONTS['axis'])
axl.set_ylabel(left_long_name+' ['+left_units+']', fontproperties=theme.FONTS['axis'])
theme.set_axis_colors(axl)
axr.set_ylabel(right_long_name+' ['+right_units+']', fontproperties=theme.FONTS['axis']
,rotation=-90, verticalalignment='bottom')
theme.set_axis_colors(axr)
axl.text(
0.5,
0.95,
left_long_name,
horizontalalignment='center',
color=theme.COLOURS['time series'][left_variable],
fontproperties=theme.FONTS['legend label large'],
transform=axl.transAxes)
axl.text(
0.5,
0.9,
right_long_name,
horizontalalignment='center',
color=theme.COLOURS['time series'][right_variable],
fontproperties=theme.FONTS['legend label large'],
transform=axl.transAxes)
axl.grid(axis='x')
make_figure()
Function¶This is is the function that will be called by the nowcast.workers.make_plots
worker to return a matplotlib.figure.Figure
object.
def make_figure(xr_dataset,
left_variable,
right_variable,
place,
figsize=(20, 8),
theme=nowcast.figures.website_theme):
plot_data = _prep_plot_data(xr_dataset, left_variable, right_variable, place)
fig, axl, axr = _prep_fig_axes(figsize, theme)
_plot_timeseries(axl, plot_data.left, left_variable, theme)
_plot_timeseries(axr, plot_data.right, right_variable, theme)
_timeseries_axes_labels(axl, axr, left_variable, plot_data.left_long_name, plot_data.left_units,
right_variable, plot_data.right_long_name, plot_data.right_units, theme)
return fig
%%timeit -n1 -r1
reload(nowcast.figures.website_theme) # to reload website theme colours etc
fig = make_figure(xr_dataset, left_variable, right_variable, place)
20.3 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)
left_variable = 'mesozooplankton'
right_variable = 'microzooplankton'
%%timeit -n1 -r1
reload(nowcast.figures.website_theme) # to reload website theme colours etc
fig = make_figure(xr_dataset, left_variable, right_variable, place)
44.8 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)
left_variable = 'ciliates'
right_variable = 'flagellates'
%%timeit -n1 -r1
reload(nowcast.figures.website_theme) # to reload website theme colours etc
fig = make_figure(xr_dataset, left_variable, right_variable, place)
7.19 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)
xr_dataset = xr.open_dataset('https://salishsea.eos.ubc.ca/erddap/griddap/ubcSSg3DTracerFields1hV17-02')
left_variable = 'temperature'
right_variable = 'salinity'
%%timeit -n1 -r1
reload(nowcast.figures.website_theme) # to reload website theme colours etc
fig = make_figure(xr_dataset, left_variable, right_variable, place)
46.3 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)