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,

Tim

import cdstoolbox as ct

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

@ct.output.livefigure()

def application():
    data = ct.catalogue.retrieve(
        'reanalysis-era5-land-monthly-means',
        {
            'variable': '2m_temperature',
            'product_type': 'monthly_averaged_reanalysis',
            'year': [
                '2002','2003','2004','2005','2006','2007','2008','2009','2010','2011','2012',
            ],
            'month': [
                '01','02','03','04','05','06','07','08','09','10','11','12'
            ],
            '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',
            'xaxis':{
                'title': 'Time [years]',
                'range': ['2001-07','2013-06']
            },
            'yaxis': {
              'title': 'ERA5 2m Air temperature (°C)',               
              'range': [-10,1]
            }
        },
        scatter_kwargs = {
            'mode': 'lines'
        }
    ) 
    
    return fig


2 Comments

  1. Dear Tim,

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

    https://cds.climate.copernicus.eu/toolbox-editor/168/forum_trend_line

    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(
            temp,
            date_range_kwargs=
            {
                'start': start_date, 
                'end': end_date, 
                'periods': 2
            },
            func='linear'
        )

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

    Regards.

    Vivien

  2. Dear Vivien,

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

    Thanks and regards,

    Tim