Marker size based on value
Mostrar comentarios más antiguos
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 comentarios
Stephen23
el 17 de Feb. de 2016
Can you please upload your data so that we can try your code. Click the paperclip button, then both Choose file and Attach file buttons.
Gareth Maver
el 17 de Feb. de 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:
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Line Plots en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!