Plotting a trend line

Hello guys,

im struggling with the following problem, maybe someone can help.

My plot (see the code below) is about the timeseries of a variable and i want to add a linear trend. What i already did is to figure out the necessary parameters a and b.

Is there a toolbox-function to plot a line with just these paramets requiered? Or is creating a vector out of these parameters and plotting this vector a better solution?

Any help is appreciated!

Thanks and cheers,


 Expand source
import cdstoolbox as ct

@ct.application(title=‘Zonal mean and trend’)


def application():
data = ct.catalogue.retrieve(
‘variable’: ‘2m_temperature’,
‘product_type’: ‘monthly_averaged_reanalysis’,
‘year’: [
‘month’: [
‘time’: ‘00:00’,

temp = ct.cube.average(data, dim=['lat', 'lon'])

a, b, a_std, b_std = ct.stats.trend(
    temp, slope_units='°C year-1')
fig  = ct.chart.line(temp,
    layout_kwargs = {
        'title': 'Monthly average temperature',
            'title': 'Time [years]',
            'range': ['2001-07','2013-06']
        'yaxis': {
          'title': 'ERA5 2m Air temperature (°C)',               
          'range': [-10,1]
    scatter_kwargs = {
        'mode': 'lines'

return fig</pre>

Dear Tim,

I have added a trend line to your workflow in the following application:

The trick is to compute the linear fit of the your data using the ct.stats.extrapolate function and plot the fit.

fit = ct.stats.extrapolate(
            'start': start_date, 
            'end': end_date, 
            'periods': 2

Let me know if you have any question or want any further explanation.



Dear Vivien,

thanks, this is exactly what I want to plot. No further questions!

Thanks and regards,