很多朋友可能喜歡在GEE中直接進行某些資料的統計和出圖,是以今天我們就來講講直方圖的統計和繪制。
首先看看主要用到的GEE當中的方法:
ui.Chart.image.histogram(image, region, scale, maxBuckets, minBucketWidth, maxRaw, maxPixels)
Generates a Chart from an image. Computes and plots histograms of the values of the bands in the specified region of the image.
- X-axis: Histogram buckets (of band value).//恒後一般是你的波段
- Y-axis: Frequency (number of pixels with a band value in the bucket).//相應波段的頻率
Returns a chart.
Arguments:
image (Image):
The image to generate a histogram from.
region (Feature|FeatureCollection|Geometry, optional):
The region to reduce. If omitted, uses the entire image.
scale (Number, optional):
The pixel scale used when applying the histogram reducer, in meters.
maxBuckets (Number, optional):
The maximum number of buckets to use when building a histogram; will be rounded up to a power of 2.
建構直方圖時使用的最大桶數,也就是列數;将四舍五入到 2 的幂。
minBucketWidth (Number, optional):
The minimum histogram bucket width, or null to allow any power of 2.
最小直方條寬度,或 null 以允許任何 2 的幂。
maxRaw (Number, optional):
The number of values to accumulate before building the initial histogram.
在建構初始直方圖之前要累積的值的數量。
maxPixels (Number, optional):
If specified, overrides the maximum number of pixels allowed in the histogram reduction. Defaults to 1e6.
如果指定,則覆寫直方圖縮減中允許的最大像素數。預設為 1e6
Returns: ui.Chart
此外還有一個功能要說:
setOptions(options)用于設定你要選擇的什麼圖形,并由很多參數可以設定
DEPRECATED: Use ui.Chart.* instead.目前用這個替代
Sets options used to style this chart.
Returns a new Chart with the passed-in options.
Arguments:
this:chart (Chart):
The Chart instance.
options (Object):
An object defining chart style options such as:
- title (string) The title of the chart.
标題(字元串)圖表的标題
- colors (Array) An array of colors used to draw the chart. Its format should follow the Google Visualization API's
顔色(數組)用于繪制圖表的顔色數組。其格式應遵循 Google Visualization API 的選項:
options: https://developers.google.com/chart/interactive/docs/customizing_charts
可以将下面的顔色換成
colors: ['#e0440e', '#e6693e', '#ec8f6e', '#f3b49f', '#f6c7b6']
還可以設定長寬:
width: 400,
height: 240,
Returns: Chart
這次用到的資料主要是DEM資料,而且以矩形的形式圈定了一塊範圍,最後分别統計不同高程處的影像數量。代碼很簡單:
var elevation = ee.Image('CGIAR/SRTM90_V4');
var colorado = ee.Geometry.Rectangle({
coords: [-109.05, 37, -102.05, 41],
geodesic: false
});
// 生成直方圖資料. 設定直方圖每一條的寬度是多少,或者是2的N次方
var histogram = ui.Chart.image.histogram({
image: elevation,
region: colorado,
scale: 200,
minBucketWidth: 300
});
histogram.setOptions({
width: 400,
height: 240,
title: 'Histogram of Elevation in Colorado (meters)'
colors: ['#e0440e', '#e6693e', '#ec8f6e', '#f3b49f', '#f6c7b6']
});
print(histogram);
Map.addLayer(elevation.clip(colorado));
Map.setCenter(-107, 39, 6);
這是選擇的美國科羅拉多州的DEM看起來不清晰,因為沒有使用相應的色調進行描繪
換了色彩之後是這樣的