1 效果图
2 代码
clc,clear
load('shpdata.mat')
% save('shpdata','SHP_DATA')
colour_totall=512
shp_data = SHP_DATA.shp_data
province_data=shaperead('maps/bou2_4p.shp', 'UseGeoCoords', true);% 省
%将省界多边形属性转为线条属性,否则会被覆盖
province_len = length(province_data)
for i=1:province_len
province_data(i).Geometry = 'Line'
end
num=length(shp_data)
%本地图太老没有 香港 澳门 营口 襄阳四个城市
except_city = {'香港' '澳门' '营口' '襄阳' '台湾'}
except_num=length(except_city)
d=importdata('temperature.txt');
data=d.data; % 温度数据
city_arr=d.textdata; % 相对应的市名称
datanum = length(data)
mycolormap=hot(colour_totall);
%将颜色反序
mycolormap = inverted_color(mycolormap)
% geo_name={shp_data.NAME}';
for j = 1:num
geo_name{j} = shp_data(j).VARNAME_2
end
max_temperature = max(data)+2;
min_temperature = min(data)-2
n=datanum; % 市数量
symbol_spec=cell(1,n-except_num); % 预定义变量可以加快处理速度
symbol_index=1
for i=1:n
tmp_pn=city_arr{i};
%过滤掉四个不在地图内的城市
if ~isempty(findstr(tmp_pn,except_city{1})) || ~isempty(findstr(tmp_pn,except_city{2})) ...
~isempty(findstr(tmp_pn,except_city{3})) || ~isempty(findstr(tmp_pn,except_city{4}))
continue
elseif ~isempty(findstr(tmp_pn,except_city{5}))
taiwan_temperature = data(i)
continue
end
temperature_t=data(i);
temperature_t = temperature_t+abs(min_temperature)
c_index=floor( colour_totall * temperature_t / (max_temperature-min_temperature) );
c_index(c_index<1)=1;
geoidx= city_cmp(tmp_pn,geo_name)
% if symbol_index==334
% t=1
% end
if geoidx > 0
city_name=geo_name( geoidx(1) );
symbol_spec{symbol_index} = {'VARNAME_2', char(city_name), 'FaceColor', mycolormap( c_index, :) };
symbol_index = symbol_index+1
end
end
%增加台湾地区的温度,将台湾地区视为一个温度
%shp 中第345 -- 366个元素 共22个
temperature_t = taiwan_temperature+abs(min_temperature)
c_index=floor( colour_totall * temperature_t / (max_temperature-min_temperature) );
for j=345:366
symbol_spec{symbol_index} = {'VARNAME_2', char(geo_name(j)), 'FaceColor', mycolormap( c_index, :) };
symbol_index = symbol_index+1
end
% 显示地图
figure
ax=worldmap('china'); % 使用worldmap的坐标轴作图
setm(ax,'grid','off') % 关闭grid
setm(ax,'frame','off') % 关闭边框
setm(ax,'parallellabel','off') % 关闭坐标轴标记
setm(ax,'meridianlabel','off') % 关闭坐标轴标记
% 最关键的两个语句
symbols=makesymbolspec('Polygon',{'default','FaceColor',[0.9 0.9 0.8],...
'LineStyle','-','LineWidth',0.5,...
'EdgeColor',[0 0 0]},...
symbol_spec{:}...
);
% subplot(121)
geoshow(shp_data,'SymbolSpec',symbols);
% 图的标注
% 在图像右侧显示bar
colormap(mycolormap)
hcb=colorbar('EastOutside');
step=round((max_temperature-min_temperature)/11);
set(hcb,'YTick',(0:.1:1))
set(hcb,'YTickLabel',num2cell(min_temperature:step:max_temperature+2))
% 给图像加标题
title('中国各省市地区温度分布图(单位:℃)')
geoshow(province_data,'Color', 'black','linewidth',1.5)
3说明
1、网上有关于全国各省的温度分布的博客,但是只绘制了各省的数据图,于是在网上下载了一份包含市、县的地图,由于是国外网站下载的,其中省市名称汉字显示乱码,没办别的办法,硬着头皮,将shp文件读取后存成一个对象文件shpdata.mat,手动将市区的乱码给改写了,因为没找到修改乱码的办法。。。
2、代码中最关键的地方在于首先画各市的地图的时候,画完需要把省界加上去,这个时候如果不修改province_data的Geometry属性’Polygon’,为‘line’,那么第二次调用gepshow的时候就会覆盖上一次图,很好理解,第一次一个位置画了个多边形,第二次又画了一个大小一样的,当然覆盖了,所以想要显示省界,必须把第二次画的图属性改为’line’