Legend colors do not match plot colors.

4 ビュー (過去 30 日間)
Jason Brown
Jason Brown 2019 年 1 月 29 日
コメント済み: Kevin Phung 2019 年 1 月 31 日
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 件のコメント
Jason Brown
Jason Brown 2019 年 1 月 31 日
編集済み: Walter Roberson 2019 年 1 月 31 日
Kevin Phung
Kevin Phung 2019 年 1 月 31 日
are the dates not in order?

サインインしてコメントする。

採用された回答

Kevin Phung
Kevin Phung 2019 年 1 月 31 日
編集済み: Kevin Phung 2019 年 1 月 31 日
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 件のコメント
Jason Brown
Jason Brown 2019 年 1 月 31 日
Thank you!! Obviously, I am new to MATLAB and coding in general. I appreciate you taking the time to shorten the code.
Kevin Phung
Kevin Phung 2019 年 1 月 31 日
you're welcome :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGeographic Plots についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by