Clear Filters
Clear Filters

Color bar range color

4 views (last 30 days)
israel cohen
israel cohen on 2 Apr 2024
Commented: Voss on 14 Apr 2024
%% Location Map
This is my code. i want to create color bar from 0 to 360
and each dot color in my plot will be match to the value from D.
I mean that if dot number 1 as value from D of 180 degrees so the color of the dot will be the colors of 180 from the color bar that should go from 0 to 360.
Hope i explained my problem properly
Thank you!!!
this is the code:
Data.Task_Raw2 = readtable(L);
S = unique(Data.Task_Raw2(:, 1), 'rows', 'stable');
Data.Task_Raw = readmatrix(L2);
lat = unique(Data.Task_Raw(:, 13), "rows", 'stable');
lon = unique(Data.Task_Raw(:, 14), "rows", 'stable');
Data.Task_Raw3 = readmatrix(L3);
mag= Data.Task_Raw3(:, 19),"rows", 'stable';
% Use original lat and lon columns
original_lat = Data.Task_Raw(:, 7);
original_lon = Data.Task_Raw(:, 8);
Data.Task_Raw4 = readtable(L4);
D=Data.Task_Raw4(:, 12);
% Create a table with unique latitudes and corresponding longitudes
unique_data = table(original_lat, original_lon, 'VariableNames', {'Lat', 'Lon'});
% Find unique rows in the order they appear in the original data
unique_data = unique(unique_data, 'rows', 'stable');
% Ensure we have 31 unique values
if size(unique_data, 1) < 31
% Duplicate values for events 30 and 31
unique_data = [unique_data; unique_data(21, :); unique_data(end, :)];
end
% Convert the S table column to a cell array of strings
S_cell = table2cell(S);
% Generate 31 different colors for each dot
num_points = size(unique_data, 1);
dot_colors = jet(num_points);
figure(2)
m = [unique_data.Lat unique_data.Lon];
ax = geoaxes;
geobasemap(ax, 'satellite');
hold on;
% Generate distinct colors using the HSV color space
hue_values = linspace(0, 1, num_points);
saturation = 0.8; % You can adjust saturation if needed
value = 0.8; % You can adjust value if needed
dot_colors = hsv2rgb([hue_values' repmat(saturation, num_points, 1) repmat(value, num_points, 1)]);
% Plot the station points with different marker sizes and display station names
scatter_handles = gobjects(num_points, 1);
for i = 1:num_points
scatter_handles(i) = geoplot(m(i, 1), m(i, 2), 'o', ...
'MarkerSize', 5, 'MarkerFaceColor', dot_colors(i, :), 'MarkerEdgeColor', 'none', 'DisplayName', sprintf('Event %d:mag %.4f', i, m(i, 1), m(i, 2)));
% Add event number as text annotation next to each point
text(m(i, 1), m(i, 2), num2str(i), 'Color', 'w', 'FontSize', 10, 'HorizontalAlignment', 'center', 'VerticalAlignment', 'middle');
end
% Extract magnitude values from mag vector
mag_values = Data.Task_Raw3(:, 19);
% Plot the station points with different marker sizes and display station names
scatter_handles = gobjects(num_points, 1);
for i = 1:num_points
% Get magnitude value for current point
current_mag = mag_values(i);
% Plot the point with marker size based on magnitude value
scatter_handles(i) = geoplot(m(i, 1), m(i, 2), 'o', ...
'MarkerSize', current_mag, 'MarkerFaceColor', dot_colors(i, :), 'MarkerEdgeColor', 'none', 'DisplayName', sprintf('Event %d: Lat %.4f, Lon %.4f', i, m(i, 1), m(i, 2)));
% Add event number as text annotation next to each point
text(m(i, 1), m(i, 2), num2str(i), 'Color', 'w', 'FontSize', 10, 'HorizontalAlignment', 'center', 'VerticalAlignment', 'middle');
end
% Create a legend for dot color and event information
legend(ax, scatter_handles,'Numcolumns',2, 'Location', 'southwest');
% Set axis limits
sea_of_galilee_lat = 32.75;
sea_of_galilee_lon = 35.5;
zoom_level = 0.15;
geolimits(ax, [sea_of_galilee_lat - zoom_level, sea_of_galilee_lat + zoom_level], ...
[sea_of_galilee_lon - zoom_level, sea_of_galilee_lon + zoom_level]);

Answers (1)

Voss
Voss on 2 Apr 2024
Edited: Voss on 14 Apr 2024
The easiest way to do that is to use geoscatter, which allows you to specify colors as indices into the colormap. See this line in the geoscatter documentation for specifying the color parameter C:
  • Vector — Use different colors for each marker and linearly map values in C to the current colormap. The length of C must equal the length of lat and lon. To change the colormap for the axes, use the colormap function.
Here's a demonstration using random data:
% random data
Data = struct( ...
'Task_Raw',[NaN(31,6) 32.6+0.3*rand(31,1) 35.35+0.3*rand(31,1) NaN(31,6)], ...
'Task_Raw3',42*rand(31,19), ...
'Task_Raw4',array2table(360*rand(31,12)));
% Use original lat and lon columns
original_lat = Data.Task_Raw(:, 7);
original_lon = Data.Task_Raw(:, 8);
D = Data.Task_Raw4{:, 12}
D = 31x1
307.9098 47.9602 3.9848 39.3502 182.2855 158.9368 150.7977 81.1509 317.4943 123.5750
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% Create a table with unique latitudes and corresponding longitudes
unique_data = table(original_lat, original_lon, 'VariableNames', {'Lat', 'Lon'});
% Find unique rows in the order they appear in the original data
unique_data = unique(unique_data, 'rows', 'stable');
% Ensure we have 31 unique values
if size(unique_data, 1) < 31
% Duplicate values for events 30 and 31
unique_data = [unique_data; unique_data(21, :); unique_data(end, :)];
end
num_points = size(unique_data, 1);
num_colors = 256;
figure
m = [unique_data.Lat unique_data.Lon];
ax = geoaxes;
geobasemap(ax, 'satellite');
hold on;
dot_colors = flipud(turbo(num_colors));
% Extract magnitude values from mag vector
mag_values = Data.Task_Raw3(:, 19);
% Plot the station points with different marker sizes and display station names
scatter_handles = geoscatter(m(:, 1), m(:, 2), mag_values.^2, D, 'filled', 'o');
% Add event number as text annotation next to each point
text(m(:, 1), m(:, 2), compose('%d',1:num_points), 'Color', 'w', 'FontSize', 10, 'HorizontalAlignment', 'center', 'VerticalAlignment', 'middle');
% Set axis limits
sea_of_galilee_lat = 32.75;
sea_of_galilee_lon = 35.5;
zoom_level = 0.15;
geolimits(ax, [sea_of_galilee_lat - zoom_level, sea_of_galilee_lat + zoom_level], ...
[sea_of_galilee_lon - zoom_level, sea_of_galilee_lon + zoom_level]);
% colorbar/colormap/color-limits
colorbar
colormap(dot_colors)
clim([0 360])
  4 Comments
israel cohen
israel cohen on 14 Apr 2024
I want that the color bar will go from red to orange to blue in smother transition between the colors in the color bar. And the color bar will remain from 0 to 360
Thank you!!
Voss
Voss on 14 Apr 2024
I have edited the answer to use a turbo colormap, which goes from red to orange to blue, with 256 colors.

Sign in to comment.

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!