Legend colors do not match plot colors.
5 views (last 30 days)
Show older comments
I have an Excel file (attached) that contains the latitude and longitude, starting and ending, for 198 tornadoes that occured between 2008 and 2018. I have a program (below) that plots each set of coordinates and colors it according to year. However, the legend colors do not match the plot colors. I have checked several of the questions posted here, but none seem to deal with for loops and if statements. Any help would be greatly appreciated! An if anyone can provide guidance on adding a scale ruler to this project, I would be greatful.
c01 = [0.2422 0.1504 0.6603];
c02 = [0.2803 0.2782 0.9221];
c03 = [0.2440 0.4358 0.9988];
c04 = [0.1540 0.5902 0.9218];
c05 = [0.0297 0.7082 0.8163];
c06 = [0.1938 0.7758 0.6251];
c07 = [0.5044 0.7993 0.3480];
c08 = [0.8634 0.7406 0.1596];
c09 = [0.9892 0.8136 0.1885];
c10 = [0.9769 0.9839 0.0805];
c11 = [0.2791 0.2625 0.9064];
data = xlsread('Tornado Data - 4th Clean No Duplicate Entries.xlsm');
for x = 1:length(data)
if data(x,2) == 2008
geoplot([data(x,8) data(x,10)],[data(x,9) data(x,11)],...
'LineWidth',0.5,'Color',c01,'Marker','*')
hold on;
elseif data(x,2) == 2009
geoplot([data(x,8) data(x,10)],[data(x,9) data(x,11)],...
'LineWidth',0.5,'Color',c02,'Marker','*')
hold on;
elseif data(x,2) == 2010
geoplot([data(x,8) data(x,10)],[data(x,9) data(x,11)],...
'LineWidth',0.5,'Color',c03,'Marker','*')
hold on;
elseif data(x,2) == 2011
geoplot([data(x,8) data(x,10)],[data(x,9) data(x,11)],...
'LineWidth',0.5,'Color',c04,'Marker','*')
hold on;
elseif data(x,2) == 2012
geoplot([data(x,8) data(x,10)],[data(x,9) data(x,11)],...
'LineWidth',0.5,'Color',c05,'Marker','*')
hold on;
elseif data(x,2) == 2013
geoplot([data(x,8) data(x,10)],[data(x,9) data(x,11)],...
'LineWidth',0.5,'Color',c06,'Marker','*')
hold on;
elseif data(x,2) == 2014
geoplot([data(x,8) data(x,10)],[data(x,9) data(x,11)],...
'LineWidth',0.5,'Color',c07,'Marker','*')
hold on;
elseif data(x,2) == 2015
geoplot([data(x,8) data(x,10)],[data(x,9) data(x,11)],...
'LineWidth',0.5,'Color',c08,'Marker','*')
hold on;
elseif data(x,2) == 2016
geoplot([data(x,8) data(x,10)],[data(x,9) data(x,11)],...
'LineWidth',0.5,'Color',c09,'Marker','*')
hold on;
elseif data(x,2) == 2017
geoplot([data(x,8) data(x,10)],[data(x,9) data(x,11)],...
'LineWidth',0.5,'Color',c10,'Marker','*')
hold on;
else
geoplot([data(x,8) data(x,10)],[data(x,9) data(x,11)],...
'LineWidth',0.5,'Color',c11,'Marker','*')
hold on;
end
end
lgd = legend({'2008','2009','2010','2011','2012','2013','2014','2015','2016','2017','2018'},'Location','eastoutside');
title(lgd,'Year')
geobasemap('colorterrain')
title('Tornado Strikes 2008 - 2018')
2 Comments
Accepted Answer
Kevin Phung
on 31 Jan 2019
Edited: Kevin Phung
on 31 Jan 2019
You can also shorten your code:
c01 = [0.2422 0.1504 0.6603];
c02 = [0.2803 0.2782 0.9221];
c03 = [0.2440 0.4358 0.9988];
c04 = [0.1540 0.5902 0.9218];
c05 = [0.0297 0.7082 0.8163];
c06 = [0.1938 0.7758 0.6251];
c07 = [0.5044 0.7993 0.3480];
c08 = [0.8634 0.7406 0.1596];
c09 = [0.9892 0.8136 0.1885];
c10 = [0.9769 0.9839 0.0805];
c11 = [0.2791 0.2625 0.9064];
color = [c01;c02;c03;c04;c05;c06;c07;c08;c09;c10;c11];
data = xlsread('Tornado Data - 4th Clean No Duplicate Entries.xlsm');
dates = data(:,2);
[sorted_dates order]=sort(dates)
data=data(order,:) % rearrages your rows to be in order;
for i = 1:size(data,1) % for each row
geoplot([data(i,8 data(i,10],[data(i,9) data(i,11)]),...
'LineWidth',0.5,'Color',color(i),'Marker','*');
hold on;
end
lgd = legend({'2008','2009','2010','2011','2012','2013','2014','2015','2016','2017','2018'},'Location','eastoutside');
title(lgd,'Year')
geobasemap('colorterrain')
title('Tornado Strikes 2008 - 2018')
2 Comments
More Answers (0)
See Also
Categories
Find more on Geographic Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!