0

我有一张用标签绘制点的地图。现在,在标签靠得很近的地方,标签会重叠。我看到有些人建议使用 textbp (FileExchange),但我从单元格数组中的列中获取标签,因此该函数不起作用。我怎样才能使标签放置在离点足够远的地方,以便清楚地看到所有标签?如果不清楚哪个标签适用于哪个情节,也许可以添加箭头?

数据是这个文件: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重叠方式绘制标签的脚本部分。

这是两个子图。 正如你所看到的,有很多点的部分是一团黑色

4

1 回答 1

2

您的代码没有按原样运行,没有 datalat_Acc 就挂断了。我不知道那是否在其中一个文件中。但是,您可以通过在制作文本标签时迭代并动态更改它们的位置来解决此问题。写出第一个标签,获取该标签的位置,并将第二个标签的位置设置为略低于它。然后你得到第二个标签的位置,并用它来调整第三个,依此类推。您还可以使用这些位置在标签和地图点之间画一条线。在这个例子中,我让它们都指向同一个地方,但是如果你根据你的代码调整它,你可以改变它。

clf
hold on
plot(peaks)
h=text(5,8,'label1', 'FontSize',8);
posh=get(h,'position');

for i=1:5
    h2=text(posh(1),posh(2)-0.5,['label',num2str(i+1)], 'FontSize',8);
    posh=get(h2,'position');

    plot([posh(1) 0],[posh(2) 7])
end

我确信有一种方法可以在不遍历所有这些的情况下做到这一点,但这是最容易编写的。

于 2013-10-11T21:15:44.370 回答