function visualizeTableMask(data,idx) figure imagesc(idx) xticklabels(erase(data.Properties.VariableNames,"_")) xticks(1:width(data)) xtickangle(-45) ys = yticks; yticklabels(cellstr(data.Time(ys))) colormap gray end function plotEventCostsMap(data,threshold) ev = ["Flood","Lightning","Tropical Storm","Hurricane",... "Waterspout","Tornado"]; idx = ismember(string(data.event_type),ev) & ... data.damage_total > threshold; x = data(idx ,:); x.weathercats = removecats(x.weathercats); x = FillMissingLatLon(x); figure gb = geobubble(x,"begin_lat","begin_lon",... "SizeVariable","damage_total","ColorVariable","weathercats"); gb.Title = "Storm Event Damage"; gb.SizeLegendTitle = "Damage Cost ($1000)"; gb.ColorLegendTitle = "Event Type"; gb.Basemap = "colorterrain"; end function data = FillMissingLatLon(data) stateLatLon = struct2table(shaperead("usastatehi")); idx = find(ismissing(data.begin_lat) & ismissing(data.begin_lon) & ~ismissing(data.state) & ... ismember(string(data.weathercats),["Tropical Storm","Hurricane",... "Waterspout"])); for ii = 1:length(idx) sidx = lower(stateLatLon.Name) == lower(string(data.state(idx(ii)))); data.begin_lat(idx(ii)) = stateLatLon.LabelLat(sidx); data.begin_lon(idx(ii)) = stateLatLon.LabelLon(sidx); end end function plotEventCosts(data) ev = ["Flood","Lightning","Tropical Storm","Hurricane",... "Waterspout","Tornado"]; idx = ismember(string(data.event_type),ev) & ... data.damage_total > 0; x = data(idx ,:); x.weathercats = removecats(x.weathercats); warning("off","MATLAB:handle_graphics:Layout:NoPositionSetInTiledChartLayout") % Create figure t = tiledlayout(4,2,"TileSpacing","compact","Padding","compact"); %#ok nexttile([1 2]) boxplot(x.damage_total,x.event_type) ylabel("Damge Total ($)") nexttile(3,[3 1]) gb = geobubble(x,"begin_lat","begin_lon",... "SizeVariable","damage_total","ColorVariable","weathercats"); gb.Title = "Storm Event Damage Total"; gb.SizeLegendTitle = "Damage Cost ($1000)"; gb.ColorLegendTitle = "Event Type"; gb.Basemap = "colorterrain"; nexttile histogram(x.damage_property) title("Property Damage ($)") nexttile histogram(x.damage_crops) title("Crop Damage ($)") nexttile scatter(x.damage_property,x.damage_crops,"."); xlabel("Property Damage ($)"); ylabel("Crop Damage ($)") sgtitle("Damage by Event") warning("on","MATLAB:handle_graphics:Layout:NoPositionSetInTiledChartLayout") end
时间: 2023-11-06 20:08:30 浏览: 151
这段代码包含了多个函数,用于可视化数据和绘制图表。以下是每个函数的功能说明:
1. `visualizeTableMask(data,idx)`: 绘制一个图表,其中使用热图显示`idx`数据,并将`data`表的变量名作为x轴标签,并将`data.Time`作为y轴标签。
2. `plotEventCostsMap(data,threshold)`: 基于给定的阈值`threshold`,绘制一个地理气泡图,显示符合特定事件类型和损失阈值的数据。地理气泡图使用经度(begin_lon)和纬度(begin_lat)来定位气泡的位置,气泡的大小表示损失总额(damage_total),气泡的颜色表示天气类别(weathercats)。
3. `FillMissingLatLon(data)`: 填充缺失的经纬度信息。根据缺失的州信息(state)和特定的天气类别,从"usastatehi"数据中查找对应州的经纬度,并将其填充到data表中对应的记录中。
4. `plotEventCosts(data)`: 绘制一个包含多个子图的图表。其中,第一个子图是箱线图,显示不同事件类型的损失总额。第二个子图是地理气泡图,显示符合特定事件类型和损失阈值的数据。第三个和第四个子图分别是直方图,显示财产损失(damage_property)和农作物损失(damage_crops)的分布情况。最后一个子图是散点图,显示财产损失和农作物损失之间的关系。
请确保在运行这些函数之前已经加载了相应的数据,并且需要提供正确的参数。
阅读全文