我有一個地圖繪製點與標籤。現在,在標籤靠得很近的地方,標籤重疊。我看到有人建議使用textbp(FileExchange),但是我從單元格數組中的列中獲取標籤,因此該函數不起作用。我怎樣才能做到這一點,所以標籤的放置距離足以讓所有標籤都清晰可見?也許添加箭頭如果不清楚哪個標籤是哪個圖的?MATLAB避免在地圖中的文本重疊
該數據是這個文件:https://www.dropbox.com/sh/li3hh1nvt11vok5/4YGfwStQlo。 我從這個文件中提取數據並對其進行排序,找到唯一的值,然後用它來繪製點。
這裏是我的腳本的一部分地圖:
%% Use function to read in 2012
% Format: data = ('filename', 'delimiter')
filename = ('PM2.5_NY_2012.csv'); % PM2.5 88101 and 88502 data from NY
data = read_mixed_csv(filename,', '); % 2012 must have ',' taken out first. DO NOT need to use for 2011
data = read_mixed_csv(filename,'"'); % Creates cell array of data (2011, 2012)
data = regexprep(data, '^"|"$',''); % Gets rid of double quotes at the start and end of the string
data = data(:,2:2:end); % 2012. Do it only if there are blank columns. Keep only the even cells because the odd ones are just commas
PM25_NY_2012 = data;
%% Pull data of a specific parameter (Latitude and Longitude - Columns 20 and 21)
% Pull out data with Local Conditions only (Locations differ compared to Acceptable PM2.5
data_Loc = data(strcmp('PM2.5 - Local Conditions', data(:,10)),:);
% Pull out data with Acceptable PM2.5 AQI only
data_Acc = data(strcmp('Acceptable PM2.5 AQI & Speciation Mass', data(:,10)),:);
%% Find index for the first unique lat and lon
% Local Conditions
[C,ia,ic] = unique(data_Loc(:,2));
DupIndex = setdiff(1:size(data_Loc(:,2)), ia);
data_Loc(DupIndex,:) = [];
datalat_Loc = data_Loc(:,19);
datalon_Loc = data_Loc(:,20);
% Acceptable PM2.5
[C, ia,ic] = unique(data_Acc(:,2));
DupIndex = setdiff(1:size(data_Acc(:,2)), ia);
data_Acc(DupIndex,:) = [];
datalat_Acc = data_Acc(:,19);
datalon_Acc = data_Acc(:,20);
%% Plot map
latlim = [39 47];
lonlim = [-81 -70];
figure('Color','w');
% Plot for Acceptable PM2.5
subplot(1,2,1)
usamap('New York'); % 'Vermont', 'Massachusetts', 'Rhode Island', 'Connecticut', 'New Jersey', 'Pennsylvania', 'Delaware', 'Maryland')
shi = shaperead('usastatehi', 'UseGeoCoords', true,...
'Selector',{@(name) strcmpi(name,'New York'), 'Name'});
geoshow(shi, 'FaceColor', [0.3 1.0, 0.675])
textm(shi.LabelLat, shi.LabelLon, shi.Name, 'HorizontalAlignment', 'center')
[row,col] = size(datalat_Acc);
nb_point = row;
LAT = str2double(datalat_Acc);
LON = str2double(datalon_Acc);
h = geoshow(LAT, LON, 'DisplayType', 'Point', 'Marker', '*', 'Color', 'red');
textm(LAT, LON,(data_Acc(:,2))', 'FontSize',8)
title('PM2.5 Sites in New York State in 2012 - Acceptable PM2.5 AQI & Speciation Mass');
hold all
% Plot for Local Conditions
subplot(1,2,2)
% figure('Color','w');
usamap('New York'); % 'Vermont', 'Massachusetts', 'Rhode Island', 'Connecticut', 'New Jersey', 'Pennsylvania', 'Delaware', 'Maryland')
shi = shaperead('usastatehi', 'UseGeoCoords', true,...
'Selector',{@(name) strcmpi(name,'New York'), 'Name'});
geoshow(shi, 'FaceColor', [0.3 1.0, 0.675])
textm(shi.LabelLat, shi.LabelLon, shi.Name, 'HorizontalAlignment', 'center')
[row,col] = size(datalat_Loc);
nb_point = row;
LAT = str2double(datalat_Loc);
LON = str2double(datalon_Loc);
h = geoshow(LAT, LON, 'DisplayType', 'Point', 'Marker', '+', 'Color', 'red');
textm(LAT, LON,(data_Loc(:,2))', 'FontSize',5)
title('PM2.5 Sites in New York State in 2012 - Local Conditions');
所以這個問題是與繪製標籤在重疊的方式腳本的textm
一部分。
我無法理解如何執行您的建議。一般來說,我對編碼相當陌生。我已經更新了我的腳本以包含缺少的部分。它應該現在運行。你可以看一下嗎?這將幫助我弄清楚具體情況與我的具體情況有關。 – shizishan
那麼它仍然不會運行沒有read_mixed_csv()。但你仍然應該能夠適應我的例子。 get(h,'position')函數返回文本標籤的數組[x座標y座標z座標]。所以你在textm()函數中使用LAT和LON值而不是x和y。所以首先你需要將你的textm(LAT,LON ...)函數改爲for-loop,然後你可以用位置(1和2)替換LAT和LON。在循環中,每次放置textm()標籤時,都可以使用lat(i)= lat(i-1) - #度的緯度值,這會將標籤向下移動,低於之前標籤的#degrees。 – jmolaro
我會盡力做到這一點,並希望能夠得到它的工作。我已將read_mixed_csv添加到文件夾中。它是由某人將其發佈爲一個Stackoverflow問題的答案,所以我忘了它。 – shizishan