ui.Chart.array.values
函式提供一種方法,可從 ee.Array
和 ee.List
物件算繪圖表。
以下範例會使用透過 ee.Reducer.toList()
減算器減少圖像頻帶和圖像中繼資料所產生的陣列和清單資料。請注意,任何沿著特定軸線具有相同長度的清單和/或陣列集合,都可以透過 ui.Chart.array.values
繪製。
ee.Array
區域散布圖
ee.Reducer.toList()
的圖片區域縮減功能會產生像素值清單字典,每個圖像的每個頻帶各有一組。這裡是用來從 MODIS 圖像中,針對與森林生態區重疊的像素,擷取紅色、近紅外線和 SWIR 反射率值清單。紅色反射率值會繪製到 X 軸,NIR 和 SWIR 值則會繪製到 Y 軸。
本例中用來劃分森林生態區的 projects/google/charts_feature_example 素材資源,是為了示範而開發。這是三個生態區多邊形的集合,其中屬性描述氣候標準。
程式碼編輯器 (JavaScript)
// Import the example feature collection and subset the forest feature. var forest = ee.FeatureCollection('projects/google/charts_feature_example') .filter(ee.Filter.eq('label', 'Forest')); // Define a MODIS surface reflectance composite. var modisSr = ee.ImageCollection('MODIS/006/MOD09A1') .filter(ee.Filter.date('2018-06-01', '2018-09-01')) .select('sur_refl_b0[0-7]') .mean(); // Reduce MODIS reflectance bands by forest region; get a dictionary with // band names as keys, pixel values as lists. var pixelVals = modisSr.reduceRegion( {reducer: ee.Reducer.toList(), geometry: forest.geometry(), scale: 2000}); // Convert NIR and SWIR value lists to an array to be plotted along the y-axis. var yValues = pixelVals.toArray(['sur_refl_b02', 'sur_refl_b06']); // Get the red band value list; to be plotted along the x-axis. var xValues = ee.List(pixelVals.get('sur_refl_b01')); // Define the chart and print it to the console. var chart = ui.Chart.array.values({array: yValues, axis: 1, xLabels: xValues}) .setSeriesNames(['NIR', 'SWIR']) .setOptions({ title: 'Relationship Among Spectral Bands for Forest Pixels', colors: ['1d6b99', 'cf513e'], pointSize: 4, dataOpacity: 0.4, hAxis: { 'title': 'Red reflectance (x1e4)', titleTextStyle: {italic: false, bold: true} }, vAxis: { 'title': 'Reflectance (x1e4)', titleTextStyle: {italic: false, bold: true} } }); print(chart);
ee.List
區域散布圖
您可以使用 ui.Chart.array.values
函式繪製兩個清單物件。根據前一個範例,代表紅色和 SWIR 反射率的 X 軸和 Y 軸值清單會以散布圖的形式顯示。
程式碼編輯器 (JavaScript)
// Get Red and SWIR value lists; to be plotted along x and y axes, respectively. // Note that the pixelVals object is defined in the previous code block. var x = ee.List(pixelVals.get('sur_refl_b01')); var y = ee.List(pixelVals.get('sur_refl_b06')); // Define the chart and print it to the console. var chart = ui.Chart.array.values({array: y, axis: 0, xLabels: x}).setOptions({ title: 'Relationship Among Spectral Bands for Forest Pixels', colors: ['cf513e'], hAxis: { title: 'Red reflectance (x1e4)', titleTextStyle: {italic: false, bold: true} }, vAxis: { title: 'SWIR reflectance (x1e4)', titleTextStyle: {italic: false, bold: true} }, pointSize: 4, dataOpacity: 0.4, legend: {position: 'none'}, }); print(chart);
ee.List
橫斷面線圖
ee.Reducer.toList()
的圖片區域縮減功能會產生像素值清單字典,每個圖像頻帶一個。如果區域是線條 (如本例所示),在所需圖像中加入緯度和經度區帶時,即可產生地理橫斷面。在此,沿著橫斷面線的經度和高度像素值清單會分別擷取為個別變數,並分別繪製至 X 軸和 Y 軸。
程式碼編輯器 (JavaScript)
// Define a line across the Olympic Peninsula, USA. var transect = ee.Geometry.LineString([[-122.8, 47.8], [-124.5, 47.8]]); // Define a pixel coordinate image. var latLonImg = ee.Image.pixelLonLat(); // Import a digital surface model and add latitude and longitude bands. var elevImg = ee.Image('NASA/NASADEM_HGT/001').select('elevation').addBands(latLonImg); // Reduce elevation and coordinate bands by transect line; get a dictionary with // band names as keys, pixel values as lists. var elevTransect = elevImg.reduceRegion({ reducer: ee.Reducer.toList(), geometry: transect, scale: 1000, }); // Get longitude and elevation value lists from the reduction dictionary. var lon = ee.List(elevTransect.get('longitude')); var elev = ee.List(elevTransect.get('elevation')); // Sort the longitude and elevation values by ascending longitude. var lonSort = lon.sort(lon); var elevSort = elev.sort(lon); // Define the chart and print it to the console. var chart = ui.Chart.array.values({array: elevSort, axis: 0, xLabels: lonSort}) .setOptions({ title: 'Elevation Profile Across Longitude', hAxis: { title: 'Longitude', viewWindow: {min: -124.50, max: -122.8}, titleTextStyle: {italic: false, bold: true} }, vAxis: { title: 'Elevation (m)', titleTextStyle: {italic: false, bold: true} }, colors: ['1d6b99'], lineSize: 5, pointSize: 0, legend: {position: 'none'} }); print(chart);
套用 .setChartType('AreaChart')
以在線條下方加入陰影:
print(chart.setChartType('AreaChart'));
ee.List
中繼資料散布圖
ee.Reducer.toList()
的收集資源屬性縮減功能會產生屬性值清單字典,每個所選屬性各有一個。在此,雲層覆蓋率和幾何 RMSE 屬性清單是從一組 Landsat 8 影像產生,並做為個別變數。雲量變數會沿著 X 軸繪製,而幾何 RMSE 則沿著 Y 軸繪製。
程式碼編輯器 (JavaScript)
// Import a Landsat 8 collection and filter to a single path/row. var col = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2') .filter(ee.Filter.expression('WRS_PATH == 45 && WRS_ROW == 30')); // Reduce image properties to a series of lists; one for each selected property. var propVals = col.reduceColumns({ reducer: ee.Reducer.toList().repeat(2), selectors: ['CLOUD_COVER', 'GEOMETRIC_RMSE_MODEL'] }) .get('list'); // Get selected image property value lists; to be plotted along x and y axes. var x = ee.List(ee.List(propVals).get(0)); var y = ee.List(ee.List(propVals).get(1)); // Define the chart and print it to the console. var chart = ui.Chart.array.values({array: y, axis: 0, xLabels: x}) .setChartType('ScatterChart') .setOptions({ title: 'Landsat 8 Image Collection Metadata (045030)', colors: ['96356f'], hAxis: { title: 'Cloud cover (%)', titleTextStyle: {italic: false, bold: true} }, vAxis: { title: 'Geometric RMSE (m)', titleTextStyle: {italic: false, bold: true} }, pointSize: 5, dataOpacity: 0.6, legend: {position: 'none'}, }); print(chart);
ee.List
對應函式散布圖和折線圖
將函式對應至 x 值清單,以便計算相應的 y 值清單。在這裡,sin()
函式會對應至 X 軸值清單,產生相應的 Y 軸值清單。繪製 x 和 y 清單時,系統會顯示正弦波的範例。
程式碼編輯器 (JavaScript)
// Define a sequence from -2pi to +2pi in 50 increments. var start = -2 * Math.PI; var end = 2 * Math.PI; var points = ee.List.sequence(start, end, null, 50); // Evaluate the sin() function for each value in the points sequence. var values = points.map(function(val) { return ee.Number(val).sin(); }); // Define the chart and print it to the console. var chart = ui.Chart.array.values({array: values, axis: 0, xLabels: points}) .setOptions({ title: 'Sine Function', hAxis: { title: 'radians', viewWindowMode: 'maximized', ticks: [ {v: start, f: '-2π'}, {v: -Math.PI, f: '-π'}, {v: 0, f: '0'}, {v: Math.PI, f: 'π'}, {v: end, f: '2π'} ], titleTextStyle: {italic: false, bold: true} }, vAxis: { title: 'sin(x)', titleTextStyle: {italic: false, bold: true} }, colors: ['39a8a7'], lineWidth: 3, pointSize: 7, viewWindow: {min: start, max: end}, legend: {position: 'none'} }); print(chart);