Plot FeatureCollection#

The geetools extension contains a set of functions for rendering charts from ee.FeatureCollection objects. The choice of function determines the arrangement of data in the chart, i.e., what defines x- and y-axis values and what defines the series. Use the following function descriptions and examples to determine the best function and chart type for your purpose.

github colab

Set up environment#

Install all the required libs if necessary and perform the import statements upstream.

# uncomment if installation of libs is necessary
# !pip install earthengine-api geetools
from matplotlib import pyplot as plt

import ee
import geetools #noqa: F401
# uncomment if authetication to GEE is needed
# ee.Authenticate()
# ee.Intialize(project="<your_project>")

Example data#

The following examples rely on a FeatureCollection composed of three ecoregion features with properties that describe climate normals.

# Import the example feature collection.
ecoregions = ee.FeatureCollection('projects/google/charts_feature_example')

Plot by features#

Features are plotted along the x-axis by values of a selected property. Series are defined by a list of property names whose values are plotted along the y-axis. The type of produced chart can be controlled by the type parameter as shown in the following examples.

If you want to use another plotting library you can get the raw data using the byFeatures function.

../../_images/5b67660222f0be3b50e6ad711172df203f4ebb9461f9c23dd545ae6915242dbc.png

See API

  • geetools.FeatureCollectionAccessor.plot_by_features not found

  • byFeatures: Get a dictionary with all property values for each feature.

Column chart#

Features are plotted along the x-axis, labeled by values of a selected property. Series are represented by adjacent columns defined by a list of property names whose values are plotted along the y-axis.

fig, ax = plt.subplots(figsize=(10, 4))

# initialize the plot with the ecoregions data
ecoregions.geetools.plot_by_features(
    type = "bar",
    featureId = "label",
    properties = ['01_tmean', '02_tmean', '03_tmean', '04_tmean', '05_tmean', '06_tmean', '07_tmean', '08_tmean', '09_tmean', '10_tmean', '11_tmean', '12_tmean'],
    labels = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"],
    colors = ['#604791', '#1d6b99', '#39a8a7', '#0f8755', '#76b349', '#f0af07', '#e37d05', '#cf513e', '#96356f', '#724173', '#9c4f97', '#696969'],
    ax = ax
)

# once created the axes can be modified as needed using pure matplotlib functions
ax.set_title("Average Monthly Temperature by Ecoregion")
ax.set_xlabel("Ecoregion")
ax.set_ylabel("Temperature (°C)")
plt.show()
../../_images/57c805367613a7c83914af128c78da0f655906fbfcc0d5ef9278d7752043bb75.png

Stacked column chart#

Features are plotted along the x-axis, labeled by values of a selected property. Series are represented by stacked columns defined by a list of property names whose values are plotted along the y-axis as the cumulative series sum.

fig, ax = plt.subplots(figsize=(10, 4))

# initialize theplot with the ecoregions data
ecoregions.geetools.plot_by_features(
    type = "stacked",
    featureId = "label",
    properties = ['01_ppt', '02_ppt', '03_ppt', '04_ppt', '05_ppt', '06_ppt', '07_ppt', '08_ppt', '09_ppt', '10_ppt', '11_ppt', '12_ppt'],
    labels = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"],
    colors = ['#604791', '#1d6b99', '#39a8a7', '#0f8755', '#76b349', '#f0af07', '#e37d05', '#cf513e', '#96356f', '#724173', '#9c4f97', '#696969'],
    ax = ax
)

# once created the axes can be modified as needed using pure matplotlib functions
ax.set_title("Average Monthly Precipitation by Ecoregion")
ax.set_xlabel("Ecoregion")
ax.set_ylabel("Precipitation (mm)")
plt.show()
../../_images/5ef2c58fbb7dd9afea7329dc949704e42e09514d1278d63fec002f43d80a4599.png

Scatter chart#

Features are plotted along the x-axis, labeled by values of a selected property. Series are represented by points defined by a list of property names whose values are plotted along the y-axis.

fig, ax = plt.subplots(figsize=(10, 4))

# initialize theplot with the ecoregions data
ecoregions.geetools.plot_by_features(
    type = "scatter",
    featureId = "label",
    properties = ['01_ppt', '06_ppt', '09_ppt'],
    labels = ["jan", "jun", "sep"],
    ax = ax
)

# once created the axes can be modified as needed using pure matplotlib functions
ax.set_title("Average Monthly Precipitation by Ecoregion")
ax.set_xlabel("Ecoregion")
ax.set_ylabel("Precipitation (mm)")
plt.show()
../../_images/f77caeb208a23618416632e93c7ee7b4cddad75c22c34ab6023f96650e44ff9d.png

Pie chart#

The pie is a property, each slice is the share from each feature whose value is cast as a percentage of the sum of all values of features composing the pie.

fig, ax = plt.subplots(figsize=(10, 4))

# initialize theplot with the ecoregions data
ecoregions.geetools.plot_by_features(
    type = "pie",
    featureId = "label",
    properties = ['06_ppt'],
    colors = ["#f0af07", "#0f8755", "#76b349"],
    ax = ax
)

# once created the axes can be modified as needed using pure matplotlib functions
ax.set_title("Share of precipitation in June by Ecoregion")
plt.show()
../../_images/20e016b5123ee52c8fc3af69c91766a35df7425aa6995aeaa3c0245a9ce47b29.png

Donut chart#

The donut is a property, each slice is the share from each feature whose value is cast as a percentage of the sum of all values of features composing the donut.

fig, ax = plt.subplots(figsize=(10, 4))

# initialize theplot with the ecoregions data
ecoregions.geetools.plot_by_features(
    type = "donut",
    featureId = "label",
    properties = ['07_ppt'],
    colors = ["#f0af07", "#0f8755", "#76b349"],
    ax = ax
)

# once created the axes can be modified as needed using pure matplotlib functions
ax.set_title("Share of precipitation in July by Ecoregion")
plt.show()
../../_images/a4837a64c66f760e6a456d3844ee4b400b1e59c02a7647ce24c78c586bdf4a9d.png

Plot by properties#

Feature properties are plotted along the x-axis by name; values of the given properties are plotted along the y-axis. Series are features labeled by values of a selected property. The type of produced chart can be controlled by the type parameter as shown in the following examples.

../../_images/dce028799f670fbe599a6a7fad6f98279ba6563f084fbebacc4f633ed65ca90e.png

See API

  • plot_by_properties: Plot the values of a :py:class:`ee.FeatureCollection` by property.

  • byProperties: Get a dictionary with all feature values for each property.

Column chart#

Feature properties are plotted along the x-axis, labeled and sorted by a dictionary input; the values of the given properties are plotted along the y-axis. Series are features, represented by columns, labeled by values of a selected property.

fig, ax = plt.subplots(figsize=(10, 4))


# initialize theplot with the ecoregions data
ax = ecoregions.geetools.plot_by_properties(
    type = "bar",
    properties = ['01_ppt', '02_ppt', '03_ppt', '04_ppt', '05_ppt', '06_ppt', '07_ppt', '08_ppt', '09_ppt', '10_ppt', '11_ppt', '12_ppt'],
    labels = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"],
    featureId = "label",
    colors = ["#f0af07", "#0f8755", "#76b349"],
    ax = ax
)

# once created the axes can be modified as needed using pure matplotlib functions
ax.set_title("Average Monthly Precipitation by Ecoregion")
ax.set_xlabel("Month")
ax.set_ylabel("Precipitation (mm)")
plt.show()
../../_images/3b974f627e33f14c8e3b640401d3bcb10be904bca8fc984f6c09d0b9b36d3931.png

Line chart#

Feature properties are plotted along the x-axis, labeled and sorted by a dictionary input; the values of the given properties are plotted along the y-axis. Series are features, represented by columns, labeled by values of a selected property.

fig, ax = plt.subplots(figsize=(10, 4))

# initialize theplot with the ecoregions data
ax = ecoregions.geetools.plot_by_properties(
    type = "plot",
    properties = ["01_ppt", "02_ppt", "03_ppt", "04_ppt", "05_ppt", "06_ppt", "07_ppt", "08_ppt", "09_ppt", "10_ppt", "11_ppt", "12_ppt"],
    featureId = "label",
    labels = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"],
    colors = ["#f0af07", "#0f8755", "#76b349"],
    ax = ax
)

# once created the axes can be modified as needed using pure matplotlib functions
ax.set_title("Average Monthly Precipitation by Ecoregion")
ax.set_xlabel("Month")
ax.set_ylabel("Precipitation (mm)")
plt.show()
../../_images/9213c22fbad4f9d69e7484da1a51e7ab4d0948ff56d549f4fd22306d099d085f.png

Area chart#

Feature properties are plotted along the x-axis, labeled and sorted by a dictionary input; the values of the given properties are plotted along the y-axis. Series are features, represented by lines and shaded areas, labeled by values of a selected property.

fig, ax = plt.subplots(figsize=(10, 4))

# initialize the plot with the ecoregions data
ax = ecoregions.geetools.plot_by_properties(
    type = "fill_between",
    properties = ["01_ppt", "02_ppt", "03_ppt", "04_ppt", "05_ppt", "06_ppt", "07_ppt", "08_ppt", "09_ppt", "10_ppt", "11_ppt", "12_ppt"],
    labels = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"],
    featureId = "label",
    colors = ["#f0af07", "#0f8755", "#76b349"],
    ax = ax
)

# once created the axes can be modified as needed using pure matplotlib functions
ax.set_title("Average Monthly Precipitation by Ecoregion")
ax.set_xlabel("Month")
ax.set_ylabel("Precipitation (mm)")
plt.show()
../../_images/3e15ac0ad8fb79f110c610f99d1236b417984d53e5ce34751436046ba707b33c.png

Plot hist#

See API

plot_hist: Plot the histogram of a specific property.

The x-axis is defined by value bins for the range of values of a selected property; the y-axis is the number of elements in the given bin.

fig, ax = plt.subplots(figsize=(10, 4))

# load some data
normClim = ee.ImageCollection('OREGONSTATE/PRISM/Norm91m').toBands()

# Make a point sample of climate variables for a region in western USA.
region = ee.Geometry.Rectangle(-123.41, 40.43, -116.38, 45.14)
climSamp = normClim.sample(region, 5000)


# initialize the plot with the ecoregions data
ax = climSamp.geetools.plot_hist(
    property = "07_ppt",
    label = "July Precipitation (mm)",
    color = '#1d6b99',
    ax = ax,
    bins = 30
)

# once created the axes can be modified as needed using pure matplotlib functions
ax.set_title("July Precipitation Distribution for NW USA")
plt.show()
../../_images/d7fcfbd8c64031de553e6503952c601cacb5eb10c7c89ea046a526fe3a69df09.png