Clear Filters
Clear Filters

Marker size based on value

32 views (last 30 days)
Gareth Maver
Gareth Maver on 17 Feb 2016
Commented: Shai Katz on 20 Nov 2020
I am wanting to recreate the following plot but with the size of the dots changing based on the magnitude (mag) value.
Here is my code so far:
data = load(filename) ;
long = data(:,1) ;
lati = data(:,2) ;
year = data(:,3) ;
mag = data(:,6) ;
for i = 1:length(mag)
if mag(i) < 3.0
mag(i) = NaN ;
lati(i) = NaN ;
long(i) = NaN ;
end
end
new_lati = zeros(length(lati),1) ;
new_long = zeros(length(long),1) ;
new_lati1 = zeros(length(lati),1) ;
new_long1 = zeros(length(long),1) ;
for i = 1:length(lati)
if new_lati(i) == 0
new_lati(i) = NaN ;
end
if new_long(i) == 0
new_long(i) = NaN ;
end
if new_lati1(i) == 0
new_lati1(i) = NaN ;
end
if new_long1(i) == 0
new_long1(i) = NaN ;
end
if year(i) > 2008
new_lati(i) = lati(i) ;
new_long(i) = long(i) ;
end
if year(i) > 2013
new_lati1(i) = lati(i) ;
new_long1(i) = long(i) ;
end
end
latlim = [33 39];
lonlim = [-102 -94];
figure
ax = usamap(latlim,lonlim) ;
set(ax, 'Visible', 'off')
states = shaperead('usastatehi',...
'UseGeoCoords', true, 'BoundingBox', [lonlim', latlim']);
geoshow(ax, states, 'FaceColor', [1.0 0.9 0.7])
lat = [states.LabelLat];
lon = [states.LabelLon];
tf = ingeoquad(lat, lon, latlim, lonlim);
hold on
h1 = linem(lati, long, 'LineStyle','none', 'LineWidth',2, 'Color','b', ...
'Marker','.', 'MarkerSize',10) ;
h2 = linem(new_lati, new_long, 'LineStyle','none', 'LineWidth',2, 'Color','[0.0 0.8 0.3]', ...
'Marker','.', 'MarkerSize',10) ;
h3 = linem(new_lati1, new_long1, 'LineStyle','none', 'LineWidth',2, 'Color','r', ...
'Marker','.', 'MarkerSize',10) ;
I have tried using scatterm instead of linem, however didnt know how to keep the corresponding colours.
Any help would be appreciated. Thanks
Gareth
  3 Comments
Gareth Maver
Gareth Maver on 17 Feb 2016
Sorry. I have attached the data now.
Stephen23
Stephen23 on 17 Feb 2016
Edited: Stephen23 on 17 Feb 2016
That code would be much more efficient with logical indices rather than using slow and bulky loops:
filename = 'CenUS_ZMAP.txt';
data = load(filename);
%
long = data(:,1);
lati = data(:,2);
year = data(:,3);
mag = data(:,6);
%
idx = mag<3.0;
long(idx) = NaN;
lati(idx) = NaN;
year(idx) = NaN;
mag(idx) = NaN;
%
new_lati = lati;
new_long = long;
new_lati(year<=2008) = NaN;
new_long(year<=2008) = NaN;
Even better would be to avoid using those NaN's, and simply keep only the values that you wish to plot:
idy = data(:,6)>=3.0;
long = data(idy,1);
lati = data(idy,2);
year = data(idy,3);
mag = data(idy,6);
%
new_lati = lati(year>2008);
new_long = long(year>2008);
new_lati1 = lati(year>2013);
new_long1 = long(year>2013);
That is much simpler, much tidier, and much faster to run than using loops to move values one-at-a-time. Although beginners seem to love using loops, there are much simpler and more efficient ways to program in MATLAB, such as code vectorization. MATLAB is a high-level language, it is not Python or C++, so leave those slow and ugly loops behind you and learn to use MATLAB's fast and efficient indexing. More tips for MATLAB beginners here:

Sign in to comment.

Accepted Answer

Mike Garrity
Mike Garrity on 17 Feb 2016
The help for scatterm says:
scatterm(LAT,LON,S,C) displays colored circles at the locations
specified by the vectors LAT and LON (which must be the same size).
So the 3rd argument is your size data and the 4th argument is your color data.
You'll probably need to scale your size data. It wants values which are in "points squared". A point is 1/72 of an inch. So if I want my largest markers to be 1 inch across, I would do something like this:
seamount = load('seamount.mat');
lat = seamount.y;
lon = seamount.x;
zrange = [min(seamount.z), max(seamount.z)];
cdata = seamount.z;
size_data = (cdata-zrange(1)+1) * 72^2 / diff(zrange);
worldmap([-49 -47.5],[-150 -147.5])
scatterm(lat, lon, size_data, cdata,'filled')
  1 Comment
Shai Katz
Shai Katz on 20 Nov 2020
I have serveral questions:
1) I want to change the the size dots in the map - I mean they are too big: How I can chage them? Marksize and Fontsize don't work.
2) Because my 'S' argument are values: How can I make a different color with colorbar depend on the data value on a mape?
3) How can I cange the fontsize of the lat and long?
4) How can I add text to lat label and long label?
Thanks!
Shai

Sign in to comment.

More Answers (0)

Categories

Find more on Colormaps 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!