from IPython.display import Image
Image(url='https://vesg.ipsl.upmc.fr/thredds/fileServer/IPSLFS/jservon/CliMAF_Notebooks_html/CliMAF-logo-small.png')
Image(url='https://vesg.ipsl.upmc.fr/thredds/fileServer/IPSLFS/jservon/CliMAF_Notebooks_html/ts_plot_example.png')
which is a shortcut for ensemble_ts_plot script:
https://climaf.readthedocs.io/en/master/scripts/ensemble_ts_plot.html
Lines:
General settings:
Text:
Titles:
Legend:
Control margins:
Horizontal and vertical lines:
from climaf.api import *
clog('critical') # min verbosity = critical < warning < info < debug = max verbosity
dat = ds(project='CMIP5', model='CNRM-CM5', experiment='historical', frequency='monthly', version='v20130101',
period='1950-2005', variable='tos', realm='ocean' )
ts_dat = ccdo(space_average(dat), operator='yearmean')
CliMAF documentation for ts_plot: https://climaf.readthedocs.io/en/master/functions_results_viewing.html?highlight=TS_PLOT#ts-plot-shortcut-for-ensemble-ts-plot
# -- Trivial example: ts_dat is a CliMAF object
myplot = ts_plot(ts_dat)
iplot(myplot)
!cat last.out
# -- Possibility to provide one, or more
# -- By default ts_plot uses the CRS of the CliMAF object for the legend
myplot = ts_plot( [ts_dat] )
iplot(myplot)
# -- It is possible to provide more than one to plot multiple time series
# -- !!! can't provide twice the same object within the same list
myplot = ts_plot( [ts_dat, fadd(ts_dat, 0.1)] )
iplot(myplot)
# -- It is possible to provide more than one to plot multiple time series
# -- Unlike with list, yoiu can provide the same object twice but with different names
myplot = ts_plot( dict(ts_dat=ts_dat, ts_dat01=fadd(ts_dat, 0.1) ) ) #ts_dat01=ts_dat
iplot(myplot)
# -- Same principle as a python dictionary but with a specified order
myplot = ts_plot( cens( dict(ts_dat=ts_dat, ts_dat01=fadd(ts_dat, 0.1) ), order=['ts_dat','ts_dat01'] ) )
iplot(myplot)
We control the size of the figure and the X/Y ratio with fig_size.
By default, fig_size = '15*5', which means 15 inches in length and 5 in height.
Let's change it for '15*10'
myplot = ts_plot(ts_dat,
fig_size = '15*10',
)
iplot(myplot)
We control the width of the margins with (left,right,bottom,top)_margin.
The value is the position of the axis within the coordinates of the frame, with:
for left_margin and right_margin:
for bottom_margin and top_margin:
myplot = ts_plot(ts_dat,
fig_size = '15*10',
left_margin = 0.1,
right_margin = 1,
bottom_margin = 0.01,
top_margin = 0.8
)
iplot(myplot)
myplot = ts_plot(ts_dat,
offset = -273.15,
# - X and Y limits
xlim = ['1951-01-01','2006-12-31'],
ylim = [17.7,18.8],
# -- X and Y labels
xlabel = 'Calendar time',
ylabel = 'Temperature (degC)',
# -- Title strings
left_string = 'Sea Surface Temperature',
right_string = 'model = '+dat.model,
left_string_fontsize = 30,
right_string_fontsize = 20
#center_string = '',
#center_string_fontsize: Center string size
)
iplot(myplot)
myplot = ts_plot(dict(ts_dat=ts_dat,
ts_dat01=fadd(ts_dat, 0.1),
ts_dat02=fadd(ts_dat, 0.2) ),
offset = -273.15,
xlim = ['1951-01-01','2006-12-31'],
ylim = [17.7,18.8],
xlabel = 'Calendar time',
ylabel = 'Temperature (degC)',
left_string = 'Sea Surface Temperature',
right_string = 'model = '+dat.model,
left_string_fontsize = 30,
right_string_fontsize = 20,
# -- Colors (use python colors: https://python-graph-gallery.com/python-colors/)
colors = ['red','blue','green'],
# -- Line widths: default is 1
lw = [5,2,5],
# -- Line styles: have a look at this page to see the values you can use to specify the line styles:
# https://matplotlib.org/3.1.0/gallery/lines_bars_and_markers/linestyles.html
# Note: if you want to use a tuple, like (0,(3,5,1,5)), you should use a string
# with values separated with dash, like '0-3-5-1-5'
linestyles=['solid','dashed','0-3-5-1-5'], #,
# -- Alphas: line opacity
# -> 0 transparent
# -> 1 opac
alphas = [0.2, 1, 1],
)
iplot(myplot)
myplot = ts_plot(ts_dat,
offset = -273.15,
xlim = ['1951-01-01','2006-12-31'],
ylim = [17.7,18.8],
xlabel = 'Calendar time',
ylabel = 'Temperature (degC)',
left_string = 'Sea Surface Temperature',
right_string = 'model = '+dat.model,
left_string_fontsize = 30,
right_string_fontsize = 20,
#center_string = '',
#center_string_fontsize: Center string size
colors = 'red',
lw = 2,
highlight_period = '1980-2000',
highlight_period_lw = 8,
)
iplot(myplot)
# - Let's start by getting an ensemble
ens_req = ds(project='CMIP6',
model='IPSL-CM6A-LR',
experiment='historical',
table='Omon',
period='1900-2010',
variable='tos',
realization='*'
)
from natsort import natsorted
ens = ens_req.explore('ensemble')
ens.order = natsorted(ens.order)
# -- Compute annual mean global average
ts_ens = ccdo(space_average(ens), operator='yearmean')
myplot = ts_plot(ts_ens,
offset = -273.15,
xlim = ['1951-01-01','2006-12-31'],
#ylim = [17.7,18.8],
xlabel = 'Calendar time',
ylabel = 'Temperature (degC)',
left_string = 'Sea Surface Temperature',
right_string = 'IPSL-CM6A-LR historical',
left_string_fontsize = 30,
right_string_fontsize = 20,
)
iplot(myplot)
We use this example to show how to add a time series to an existing ensemble, and specify line specs to a plut with many lines.
# -- We start by computing the ensemble mean
ensmean = ccdo_ens(ts_ens, operator='ensmean')
# -- The easiest thing to do is to keep the ensemble (with all the time series),
# and add the ensemble mean.
# HOWEVER, you can't add the ensemble mean of an ensemble to this same ensemble.
# We thus copy the original ensemble to create an ens4plot ensemble for the plot
ens4plot = ts_ens.copy()
# -- And add the ensemble mean like this
ens4plot['ensmean'] = ensmean
# -- ts_plot will use ens4plot.order to specify the order for the plotting.
# By default, the last element added to the ensemble is at the end of the .order list.
ens4plot.order
# Knowing this, you can specify a specific color and line width for the last element of the ensemble
# with the last value of the colors and lw lists:
myplot = ts_plot(ens4plot,
offset = -273.15,
xlim = ['1951-01-01','2006-12-31'],
#ylim = [17.7,18.8],
xlabel = 'Calendar time',
ylabel = 'Temperature (degC)',
left_string = 'Sea Surface Temperature',
right_string = 'IPSL-CM6A-LR historical',
left_string_fontsize = 30,
right_string_fontsize = 20,
colors = ['grey']*len(ts_ens) + ['blue'],
lw = [1]*len(ts_ens) + [4]
)
iplot(myplot)
myplot = ts_plot(ens4plot,
offset = -273.15,
xlim = ['1951-01-01','2006-12-31'],
#ylim = [17.7,18.8],
xlabel = 'Calendar time',
ylabel = 'Temperature (degC)',
left_string = 'Sea Surface Temperature',
right_string = 'IPSL-CM6A-LR historical',
left_string_fontsize = 30,
right_string_fontsize = 20,
colors = ['grey']*len(ts_ens) + ['blue'],
lw = [1]*len(ts_ens) + [4],
legend_colors = ['grey', 'blue'],
legend_labels = ['IPSL-CM6A-LR members','Ensemble mean'],
legend_lw = [1, 4],
#legend_xy_pos: x,y Position of the corner of the box (by default = upper left corner). Example= “1.02,1”
#legend_loc: Choose the corner of the legend box to specify the position of the legend box; by default 2 (upper left corner), take values 1, 2, 3 or 4 (see resource loc of pyplot legend)
#legend_fontsize: Font size in the legend
#legend_ncol: Number of columns in the legend
#legend_frame: Draw the box around the legend? True/False
)
iplot(myplot)
myplot = ts_plot(ens4plot,
offset = -273.15,
xlim = ['1951-01-01','2006-12-31'],
#ylim = [17.7,18.8],
xlabel = 'Calendar time',
ylabel = 'Temperature (degC)',
left_string = 'Sea Surface Temperature',
right_string = 'IPSL-CM6A-LR historical',
left_string_fontsize = 30,
right_string_fontsize = 20,
colors = ['grey']*len(ts_ens) + ['blue'],
lw = [1]*len(ts_ens) + [4],
legend_colors = ['grey', 'blue'],
legend_labels = ['IPSL-CM6A-LR members','Ensemble mean'],
legend_lw = [1, 4],
legend_xy_pos= [0.02,0.94],
legend_fontsize = 20,
#legend_ncol: Number of columns in the legend
#legend_frame: Draw the box around the legend? True/False
#center_string = '',
#center_string_fontsize: Center string size
#colors = 'red',
#lw = 2,
# highlight_period = '1980-2000',
# highlight_period_lw = 8,
)
iplot(myplot)
myplot = ts_plot(ens4plot,
offset = -273.15,
xlim = ['1951-01-01','2006-12-31'],
#ylim = [17.7,18.8],
xlabel = 'Calendar time',
ylabel = 'Temperature (degC)',
left_string = 'Sea Surface Temperature',
right_string = 'IPSL-CM6A-LR historical',
left_string_fontsize = 30,
right_string_fontsize = 20,
colors = ['grey']*len(ts_ens) + ['blue'],
lw = [1]*len(ts_ens) + [4],
legend_colors = ['grey', 'blue'],
legend_labels = ['IPSL-CM6A-LR members','Ensemble mean'],
legend_lw = [1, 4],
legend_xy_pos= [0.02,0.94],
legend_fontsize = 20,
legend_ncol = 2,
legend_frame = True,
#center_string = '',
#center_string_fontsize: Center string size
#colors = 'red',
#lw = 2,
# highlight_period = '1980-2000',
# highlight_period_lw = 8,
)
iplot(myplot)
# -- We start by computing the ensemble mean
ensmean = ccdo_ens(ts_ens, operator='ensmean')
# -- The easiest thing to do is to keep the ensemble (with all the time series),
# and add the ensemble mean.
# HOWEVER, you can't add the ensemble mean of an ensemble to this same ensemble.
# We thus copy the original ensemble to create an ens4plot ensemble for the plot
ens4plot = ts_ens.copy()
# -- Highlight a specific member: r8i1p1f1 --> move it at the end of the ensemble
ens4plot.pop('r8i1p1f1')
ens4plot['r8i1p1f1'] = ts_ens['r8i1p1f1']
# -- And add the ensemble mean like this
ens4plot['ensmean'] = ensmean
ens4plot.order
myplot = ts_plot(ens4plot,
offset = -273.15,
xlim = ['1951-01-01','2006-12-31'],
#ylim = [17.7,18.8],
xlabel = 'Calendar time',
ylabel = 'Temperature (degC)',
left_string = 'Sea Surface Temperature',
right_string = 'IPSL-CM6A-LR historical',
left_string_fontsize = 30,
right_string_fontsize = 20,
colors = ['grey']*(len(ts_ens)-1) + ['red','blue'],
lw = [1]*(len(ts_ens)-1) + [2, 4],
legend_colors = ['grey', 'red', 'blue'],
legend_labels = ['IPSL-CM6A-LR members', 'r8i1p1f1', 'Ensemble mean'],
legend_lw = [1, 2, 4],
legend_xy_pos= [0.02,0.94],
legend_fontsize = 20,
legend_ncol = 2,
legend_frame = True,
)
iplot(myplot)