Hàm ui.Chart.array.values
cung cấp một phương thức để kết xuất biểu đồ từ các đối tượng ee.Array
và ee.List
.
Các ví dụ sau đây dựa vào dữ liệu mảng và danh sách được tạo bằng cách giảm các dải hình ảnh và siêu dữ liệu hình ảnh bằng cách sử dụng trình giảm ee.Reducer.toList()
. Xin lưu ý rằng ui.Chart.array.values
có thể lập biểu đồ bất kỳ tập hợp danh sách và/hoặc mảng nào có cùng độ dài dọc theo một trục nhất định.
Biểu đồ tán xạ theo khu vực ee.Array
Việc giảm vùng hình ảnh bằng ee.Reducer.toList()
sẽ tạo ra một từ điển gồm các danh sách giá trị pixel, mỗi danh sách cho một dải trong một hình ảnh nhất định. Ở đây, hàm này được dùng để trích xuất danh sách giá trị độ phản chiếu đỏ, NIR và SWIR từ hình ảnh MODIS cho các pixel giao nhau với một vùng sinh thái có rừng. Giá trị độ phản chiếu màu đỏ được biểu diễn trên trục x, giá trị NIR và SWIR được biểu diễn trên trục y.
Thành phần projects/google/charts_feature_example được dùng trong ví dụ này để xác định một vùng sinh thái có rừng, được phát triển cho mục đích minh hoạ. Đây là tập hợp gồm 3 đa giác khu sinh thái có các thuộc tính mô tả giá trị trung bình về khí hậu.
Trình soạn thảo mã (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);
Biểu đồ tán xạ theo khu vực ee.List
Bạn có thể lập biểu đồ hai đối tượng danh sách bằng cách sử dụng hàm ui.Chart.array.values
.
Dựa trên ví dụ trước, danh sách giá trị trục x và y biểu thị độ phản chiếu màu đỏ và SWIR được hiển thị dưới dạng biểu đồ tán xạ.
Trình soạn thảo mã (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);
Biểu đồ đường cắt ngang ee.List
Việc giảm vùng hình ảnh bằng ee.Reducer.toList()
sẽ tạo ra một từ điển gồm các danh sách giá trị pixel, mỗi danh sách cho một dải hình ảnh. Nếu khu vực là một đường thẳng, như trong trường hợp này, bạn có thể tạo một đường cắt địa lý khi các dải vĩ độ và kinh độ được đưa vào dưới dạng dải trong hình ảnh mà bạn quan tâm. Tại đây, danh sách giá trị pixel theo vĩ độ và độ cao dọc theo đường cắt ngang được trích xuất dưới dạng các biến riêng biệt và được lập biểu đồ tương ứng theo trục x và trục y.
Trình soạn thảo mã (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);
Áp dụng .setChartType('AreaChart')
để thêm hiệu ứng tô bóng bên dưới dòng:
print(chart.setChartType('AreaChart'));
Biểu đồ tán xạ siêu dữ liệu ee.List
Việc giảm thuộc tính của bộ sưu tập bằng ee.Reducer.toList()
sẽ tạo ra một từ điển gồm các danh sách giá trị thuộc tính, mỗi danh sách cho một thuộc tính đã chọn. Ở đây, danh sách thuộc tính độ che phủ của đám mây và RMSE hình học được tạo từ một tập hợp hình ảnh Landsat 8 dưới dạng các biến riêng biệt. Biến độ che phủ của đám mây được lập biểu đồ dọc theo trục x và RMSE hình học dọc theo trục y.
Trình soạn thảo mã (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);
Biểu đồ tán xạ và biểu đồ đường của hàm được liên kết ee.List
Ánh xạ một hàm trên danh sách các giá trị x để tính toán danh sách tương ứng của các giá trị y. Ở đây, hàm sin()
được liên kết với danh sách các giá trị trục x để tạo danh sách tương ứng các giá trị trục y. Một mẫu của sóng sin sẽ xuất hiện khi các danh sách x và y được lập biểu đồ.
Trình soạn thảo mã (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);