scatter plot with different radius for different range of values
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
*I have three column vectors , Latitude ,longitude,magnitude of earthquake (338x1),
*so I need to make a scatter plot (X axis :longitude,Y axis :latitude),
- But the radius of the scatter should be same for particular range of magnitude,like mag<3,3=<mag<4,4<=mag<5..... 9=<mag<10.
So I should read the magnitude first and plot the scatter for corresponding latitude and longitude with radius according to the magnitude range
can anyone help me with this?
Accepted Answer
michio
on 1 Sep 2016
I know it's not as elegant as we hope, but one way is to create a new variable that represents the radius and then use scatter function. logical indexing could perform better than using for-loop.
The following is one example. Hope it helps.
[x,y] = meshgrid(1:10,1:10);
% Magnitude with random number ranging from 0 to 10.
mag = rand(1,100)*10;
idx1 = mag < 3;
idx2 = (3 <= mag) & (mag < 5);
idx3 = (5 <= mag) & (mag < 8);
idx4 = 8 <= mag;
magSize = zeros(size(mag));
magSize(idx1) = 1;
magSize(idx2) = 9;
magSize(idx3) = 25;
magSize(idx4) = 49;
scatter(x(:),y(:),magSize);
8 Comments
title_name=input('Please enter period in years?\n','s')
%input for the title of the plot
for i=1:length(lat); %lat= latitude
idx1 = mag < 3;
idx2 = (3 >= mag) & (mag < 4); %mag=magnitude
idx3 = (4 >= mag) & (mag < 5);
idx4 = (5 >= mag) & (mag < 6);
idx5 = (6 >= mag) & (mag < 7);
idx6 = (7 >= mag) & (mag < 8);
idx7 = (8 >= mag) & (mag < 9);
idx8 = (mag>9);
end
magSize = zeros(size(lat));
magSize(idx1) = 10;
magSize(idx2) = 20;
magSize(idx3) = 30;
magSize(idx4) = 40;
magSize(idx5) = 50;
magSize(idx6) = 60;
magSize(idx7) = 70;
magSize(idx8) = 80;
scatter(lon,lat,magSize,'fill','MarkerEdgeColor',[0 .5 .5])%lon=longitude
hold on;
xlim([74 82]);ylim([28 35]);
xlabel('long');ylabel('latitude');
title(sprintf('%s lat vs lon :mag',title_name))
%% * _ I tried your code ( with my own improvement because it was showing some minor error) ,but still i stuck with this problem ,it seems like i only getting the scatters with same radius!! please help me to improve my code_!!* "Is it that the wrong use of '<' ,'>' sign?

michio
on 2 Sep 2016
Hmm, I would first make sure if magSize does have values as you intended. Could you try the following to see the number of entries from each maginitude category.
sum(idx1)
sum(idx2)
and so on?
vivek GB
on 7 Sep 2016
yeah magSize do have values as i intended ,i made sure that. magSize =
0
10
10
30
30
50
30
10
10
30
10
30
70
30
30
30
10
10
10
so on........
i used the below code to plot the scatter in different color for the each element in magSize have a specific value,but it seems to be loop doesnt working at all
please give your suggestion
figure
for i=1:length(lat)
if (magSize(i)==10)
scatter(lon,lat,magSize,'fill','MarkerEdgeColor','k','MarkerFaceColor','r');
hold on;
elseif magSize(i)==30
scatter(lon,lat,magSize,'fill','MarkerEdgeColor','k','MarkerFaceColor','y');
hold on;
elseif magSize(i)==50
scatter(lon,lat,magSize,'fill','MarkerEdgeColor','k','MarkerFaceColor','m');
hold on;
elseif magSize(i)==70
scatter(lon,lat,magSize,'fill','MarkerEdgeColor','k','MarkerFaceColor','c');
hold on;
elseif magSize(i)==80
scatter(lon,lat,magSize,'fill','MarkerEdgeColor','k','MarkerFaceColor','g');
hold on;
elseif magSize(i)==95
scatter(lon,lat,magSize,'fill','MarkerEdgeColor','k','MarkerFaceColor','b');
hold on;
elseif magSize(i)==105
scatter(lon,lat,magSize,'fill','MarkerEdgeColor','r','MarkerFaceColor','k');
hold on;
elseif magSize(i)==115
scatter(lon,lat,magSize,'d','fill','MarkerEdgeColor','k','MarkerFaceColor','r');
hold on;
elseif magSize(i)==115
scatter(lon,lat,magSize,'x','MarkerFaceColor','r');
hold on;
end
end
michio
on 7 Sep 2016
The above script calls scatter function for every point in each case so I do not think it's what you intended. It would be great if I could reproduce your situation at my end, it's hard to make a speculations..
Anyways, the script in my answer shows some variation in marker sizes in your side, doesn't it?
I think it's also worth pointing out that the units for the marker area is points squared. So the magSize should represent the area of each marker, not the diameter of the marker.
Having said that
scatter(lon,lat,magSize.^2,'fill','MarkerEdgeColor',[0 .5 .5])
makes any changes?
vivek GB
on 8 Sep 2016
yeah, now I can plot scatters with different size according to magSize,but I can't put different colour according to scatters size(for that I tried to use "If.....else"),how can I do that?
michio
on 8 Sep 2016
The above script of yours overwrites everything at every scatter function call. One way to do this is
scatter(lon(idx1),lat(idx1),magSize(idx1),'fill','MarkerEdgeColor','k','MarkerFaceColor','r')
hold on
scatter(lon(idx2),lat(idx2),magSize(idx2),'fill','MarkerEdgeColor','k','MarkerFaceColor','y')
scatter(lon(idx3),lat(idx3),magSize(idx3),'fill','MarkerEdgeColor','k','MarkerFaceColor','m')
scatter(lon(idx4),lat(idx4),magSize(idx4),'fill','MarkerEdgeColor','k','MarkerFaceColor','c')
scatter(lon(idx5),lat(idx5),magSize(idx5),'fill','MarkerEdgeColor','k','MarkerFaceColor','g')
and so on instead of using if else. If you use the scatter(x,y,a,c) syntax, where c specifies the circle color, it can be done by one scatter call. But if the number of category is small enough, the above way could be easier to understand.
Using the fact that scatter function accepts color as three column matrix of RGB triplets, another working example follows.
[x,y] = meshgrid(1:10,1:10);
% Magnitude with random number ranging from 0 to 10.
mag = rand(1,100)*10;
idx1 = mag < 3;
idx2 = (3 <= mag) & (mag < 5);
idx3 = (5 <= mag) & (mag < 8);
idx4 = 8 <= mag;
magSize = zeros(size(mag));
magSize(idx1) = 1;
magSize(idx2) = 9;
magSize(idx3) = 25;
magSize(idx4) = 49;
magColor = zeros(length(mag),3);
magColor(idx1,:) = repmat([1 1 0],[sum(idx1),1]);
magColor(idx2,:) = repmat([1 0 1],[sum(idx2),1]);
magColor(idx3,:) = repmat([0 1 1],[sum(idx3),1]);
magColor(idx4,:) = repmat([1 0 0],[sum(idx4),1]);
scatter(x(:),y(:),magSize,magColor,'fill');
vivek GB
on 8 Sep 2016
Thank you very much Michio,,you are such a life saver!!Got it
michio
on 8 Sep 2016
I'm glad to know it helped :)
More Answers (0)
Categories
Find more on Scatter Plots in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)