Python – How to Create UI Charts for Indices Time Series in Google Earth Engine API

google-earth-enginegoogle-maps-apipython

If I use python api for google earth engine and want to make a UI chart for mean value of NDVI for a particular year , how does the UI.CHART function work in python api for google earth engine.

Best Answer

There is a library in the universe of Jake Vanderplas that I have used to make charts in Google Earth Engine: pygal. May not be the best or newest, but it works. I have incorporated the method in a python package called ipygee. You can install it via pip.

At the moment, the only method available is series (to plot a time series), but I am about to incorporate seriesByRegion, because I need them for work, the rest will have to wait.

There is a notebook in which I show how to use it: Chart.ipynb, and the equivalent code in JS (https://code.earthengine.google.com/e50afcbdc75fbb9611a4ac7d6567fdf8)

I leave the code here as well:

# coding: utf-8
# # chart module

import ee    
import ipygee as ui

test_site = ee.Geometry.Point([-71, -42])
    
# ## Time Series
years = ee.List([2015, 2016, 2017, 2018])
    
col = ee.ImageCollection('COPERNICUS/S2').filterBounds(test_site)

def make_time_series(year):
    ''' make a time series from years list '''
    eefilter = ee.Filter.calendarRange(year, field='year')
    filtered = col.filter(eefilter)
    return filtered.mean().set('system:time_start', ee.Date.fromYMD(year, 1, 1).millis())

time_series = ee.ImageCollection(years.map(make_time_series))

# ## Chart *series*
chart_ts = ui.chart.Image.series(**{
    'imageCollection': time_series, 
    'region': test_site,
    'scale': 10,
    'bands': ['B1', 'B2', 'B3']
})

# chart_ts.renderWidget()  # for Jupyter Notebook or Lab
chart_ts.render_in_browser()  # for Spyder

As the resulting chart chart_ts is a subclass of a pygal chart, you can use all its methods, like render_to_file, etc, and all its attributes.