You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Different color scale?
96 views (last 30 days)
Show older comments
Dear all,
I have two different dataset one in txt format (ROTI output) and another in xlsx format(S4 output).I wanted to plot different scalebar with different color so that I can recognize these two data set. Would you please tell me the command of it?
Thank you,
Aramesh
4 Comments
Ara
on 25 Nov 2024 at 13:24
Edited: Ara
on 25 Nov 2024 at 13:25
Thank you for your reply.
I tries thi sfor S4:
colormap jet;
c1 = colorbar('Location', 'eastoutside');
c1.Label.String = 'S4';
caxis([0 0.3]);
and this for ROTI.
c2 = colorbar('Location', 'westoutside');
c2.Label.String = 'ROTI (TECU/min)';
caxis([0 0.5]);
but both are jet like eachother.
Mathieu NOE
on 25 Nov 2024 at 16:02
hello @Ara
it would help us (and you BTW) if you could share a working code and the data
all the best
Answers (2)
Image Analyst
on 25 Nov 2024 at 17:48
You need to use the colormap function to set the colormap for each axes independently. So display one image, set the colormap, then display the other and call colormap again with the other colormap. If you're plotting line curves instead of displaying images, there is a 'Color' (or maybe 'LineColor' option in plot to let you set the specific color of each curve.
help colormap
colormap - View and set current colormap
This MATLAB function sets the colormap for the current figure to the
specified predefined colormap.
Set Colormap
colormap map
colormap(map)
colormap(target,map)
cmap = colormap(___)
Get Current Colormap
cmap = colormap
cmap = colormap(target)
Input Arguments
map - Colormap for new color scheme
colormap name | three-column matrix of RGB triplets | 'default'
target - Target
Figure object | Axes object | PolarAxes object |
GeographicAxes object | standalone visualization
Output Arguments
cmap - Colormap values
three-column matrix of RGB triplets
Examples
openExample('graphics2/ChangeColormapUsedForPlotExample')
openExample('graphics2/ChangeColormapThenSetItBackToDefaultExample')
openExample('graphics/ColormapSpecifyAxesTiledLayoutExample')
openExample('graphics2/SpecifyNumberOfColorsForColormapExample')
openExample('graphics2/CreateCustomColormapExample')
openExample('graphics2/ReturnColormapValuesUsedInPlotExample')
openExample('graphics/ColormapReturnFromTiledLayoutExample')
openExample('graphics2/ChangeColormapForFigureWithImageExample')
See also colorbar, ind2rgb, hsv2rgb, rgbplot, Colormap Editor
Introduced in MATLAB before R2006a
Documentation for colormap
doc colormap
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
3 Comments
Ara
on 26 Nov 2024 at 7:58
Dear Image analysis,
Thank you for your response. I did this but still same color bar. And, please see atatched the figure.
I would appreciate if you help me to modefy the code?
Best.
% Add colorbar for S4
colormap(ax1, jet);
c1 = colorbar('Location', 'eastoutside');
c1.Label.String = 'S4';
caxis(ax1, [0 0.3]); % Set color axis limits for S4
% Create another map for ROTI
ax2 = worldmap(latlim, lonlim); % Axis for ROTI
setm(ax2, 'FFaceColor', 'none'); % Make the background transparent
load coastlines
geoshow(coastlat, coastlon, 'DisplayType', 'polygon', 'FaceColor', [0.6 0.8 0.6]);
hold on; % Allow overlay of ROTI data
% Plot ROTI data
idx = filtered_time_hours_all >= (i-1)*5/60 & filtered_time_hours_all < i*5/60;
if any(idx)
scatterm(all_lat(idx), all_lon(idx), 10, all_roti(idx), 'filled', 'Marker', 's');
% Add colorbar for ROTI
c2 = colorbar('Location', 'westoutside');
c2.Label.String = 'ROTI (TECU/min)';
caxis(ax2, [0 0.5]); % Set color axis limits for ROTI
end
Image Analyst
on 26 Nov 2024 at 14:44
I don't know what you want. What color ranges do you want for the left colorbar and right colorbar, and what numerical data values do the top and bottom of the colorbars represent?
Ara
on 27 Nov 2024 at 8:09
Dear Image Analysis,
I want to show different threshold for ROTI (0-0.5) and S4 (0-0.3) in one map. ALso, show different color bar. I set "jet" for one of them and "hot" for another one but it does not appear on the plot.
Walter Roberson
on 25 Nov 2024 at 18:53
caxis([0 0.3]);
That is affecting the current axis.
caxis([0 0.5]);
That too is affecting the current axis. Which appears to be the same axis as before.
Each axis has only one CLim axis property, so only one caxis() can be in effect at the same time in each axis.
If you need two different caxis() then you need two different axes.
14 Comments
Ara
on 26 Nov 2024 at 6:40
Edited: Ara
on 26 Nov 2024 at 6:43
Hi Walter,
Thank you for your response.
Exactly, I am facing this problem it set both threshould in 0.5 only. Would you please explain more how set two different axes?
I am attaching the figure.
Best,
Aramesh
Ara
on 26 Nov 2024 at 7:59
please see atatched the editted code (part of it) that i wrote.
% Add colorbar for S4
colormap(ax1, jet);
c1 = colorbar('Location', 'eastoutside');
c1.Label.String = 'S4';
caxis(ax1, [0 0.3]); % Set color axis limits for S4
% Create another map for ROTI
ax2 = worldmap(latlim, lonlim); % Axis for ROTI
setm(ax2, 'FFaceColor', 'none'); % Make the background transparent
load coastlines
geoshow(coastlat, coastlon, 'DisplayType', 'polygon', 'FaceColor', [0.6 0.8 0.6]);
hold on; % Allow overlay of ROTI data
% Plot ROTI data
idx = filtered_time_hours_all >= (i-1)*5/60 & filtered_time_hours_all < i*5/60;
if any(idx)
scatterm(all_lat(idx), all_lon(idx), 10, all_roti(idx), 'filled', 'Marker', 's');
% Add colorbar for ROTI
c2 = colorbar('Location', 'westoutside');
c2.Label.String = 'ROTI (TECU/min)';
caxis(ax2, [0 0.5]); % Set color axis limits for ROTI
end
Ara
on 26 Nov 2024 at 9:54
It means that I need to add it here like this. What to write instaed of "jet" to make a difference between them?
% Add colorbar for ROTI
colormap(ax2, jet);
c2 = colorbar('Location', 'westoutside');
c2.Label.String = 'ROTI (TECU/min)';
caxis(ax2, [0 0.5]); % Set color axis limits for ROTI
end
Ara
on 2 Dec 2024 at 9:11
Edited: Ara
on 2 Dec 2024 at 9:49
Hi Walter,
I make two different caxis but again it shows both threshould in 0.5. Do you have any clue what to do?
I would greatly appreciate your help.
Aramesh
here is my code but it still shows 0.5 for both of them.
colormap(ax, parula);
caxis([0 0.3]); % S4 range
c1 = colorbar('Location', 'eastoutside');
c1.Label.String = 'S4';
% Plot ROTI data
idx = filtered_time_hours_all >= (i-1)*5/60 & filtered_time_hours_all < i*5/60;
if any(idx)
scatterm(all_lat(idx), all_lon(idx), ...
10, all_roti(idx), ...
'filled', 'Marker', 's', 'MarkerFaceAlpha', 0.6);
colormap(ax, parula);
caxis([0 0.5]); % ROTI range
c2 = colorbar('Location', 'westoutside');
c2.Label.String = 'ROTI (TECU/min)';
Ara
on 3 Dec 2024 at 8:06
I did this but again it does not works. Show both in 0.5.
colormap(ax1, parula);
Unrecognized function or variable 'ax1'.
caxis(ax1, [0 0.3]);
c1 = colorbar('Location', 'eastoutside');
c1.Label.String = 'S4';
% Create world map for ROTI
ax2 = worldmap(latlim, lonlim);
setm(ax2, 'FFaceColor', 'none'); % Keep background transparent
hold on;
idx = filtered_time_hours_all >= (i-1)*5/60 & filtered_time_hours_all < i*5/60;
if any(idx)
scatterm(ax2, all_lat(idx), all_lon(idx), ...
10, all_roti(idx), ...
'filled', 'Marker', 's', 'MarkerFaceAlpha', 0.6);
end
colormap(ax2, parula);
caxis(ax2, [0 0.5]);
c2 = colorbar('Location', 'westoutside');
c2.Label.String = 'ROTI (TECU/min)';
Image Analyst
on 3 Dec 2024 at 14:43
Edited: Image Analyst
on 3 Dec 2024 at 14:45
It would be easier for us to reproduce and debug if you gave us a small working example, instead of an unrunnable snippet. And include screenshots of what you are getting, and if possible (mock something up in Photoshop), what you'd like to get.
Ara
on 4 Dec 2024 at 5:43
Thank you Image Analyst. Unfortunately, I cannot share my data so the code is useless without the data. I am attaching one photo of it.I just want right bar show scale from 0-0.3 (circles in the figure) and left bar show scales from 0-0.5 (dots in this figures). Is that possible to show two different scale in one map?
Walter Roberson
on 4 Dec 2024 at 8:50
colormap(ax1, parula);
caxis(ax1, [0 0.3]);
c1 = colorbar(ax1, 'Location', 'eastoutside');
c1.Label.String = 'S4';
% Create world map for ROTI
ax2 = worldmap(latlim, lonlim);
setm(ax2, 'FFaceColor', 'none'); % Keep background transparent
hold on;
idx = filtered_time_hours_all >= (i-1)*5/60 & filtered_time_hours_all < i*5/60;
if any(idx)
scatterm(ax2, all_lat(idx), all_lon(idx), ...
10, all_roti(idx), ...
'filled', 'Marker', 's', 'MarkerFaceAlpha', 0.6);
end
colormap(ax2, parula);
caxis(ax2, [0 0.5]);
c2 = colorbar(ax2, 'Location', 'westoutside');
c2.Label.String = 'ROTI (TECU/min)';
Ara
on 4 Dec 2024 at 9:17
Edited: Ara
on 4 Dec 2024 at 9:55
thanks Walter. It shows both in scale of 0.5 again.
Here is my full code. Can you please modify it here to make sure that I am doing right?
% Create a VideoWriter object for the output video
video_filename = 'Combined_S4_and_ROTI_Video_average_Parula_axes.mp4';
v = VideoWriter(video_filename, 'MPEG-4');
v.FrameRate = 2; % Adjust frame rate
open(v);
% Latitude and longitude limits for Europe
latlim = [20 90];
lonlim = [-30 60];
% Define the time range (5-minute intervals for a full day)
start_time = datetime(2023, 4, 24, 0, 0, 0);
end_time = datetime(2023, 4, 24, 23, 59, 59);
time_bins = start_time:minutes(5):end_time;
% Initialize variables for ROTI data
all_lat = [];
all_lon = [];
all_roti = [];
filtered_time_hours_all = [];
% List of ROTI data files
fileNames = {'Dataajac.txt', 'Dataasky.txt', 'Dataflrs.txt', 'Datafunc.txt', ...
'Datahamd.txt', 'Datahrmn.txt', 'Datalamp.txt', ...
'Datalpal.txt', 'Datalyns.txt', 'Datamdvj.txt', 'Datametg.txt', ...
'Datanklg.txt', 'Dataorid.txt', 'Datapolv.txt', 'Datareyk.txt', ...
'Datariga.txt', 'Datarink.txt', 'Datascor.txt', 'Datasenu.txt', ...
'Datasulp.txt', 'Datatro1.txt'};
% Process ROTI data files
for f = 1:length(fileNames)
fileName = fileNames{f};
try
% Try reading ROTI data
data = readmatrix(fileName);
time_mjd = data(:, 1); % MJD time
ROTI = data(:, 8); % ROTI values
IPP_lon = data(:, 10); % Longitude
IPP_lat = data(:, 11); % Latitude
% Convert MJD to hours since start of the day
mjd_start = floor(min(time_mjd));
time_hours = (time_mjd - mjd_start) * 24;
% Filter for specific time range (20 to 44 hours)
time_filter = time_hours >= 20 & time_hours < 44;
filtered_time_hours = time_hours(time_filter) - 20;
% Filtered data
all_lat = [all_lat; IPP_lat(time_filter)];
all_lon = [all_lon; IPP_lon(time_filter)];
all_roti = [all_roti; ROTI(time_filter)];
filtered_time_hours_all = [filtered_time_hours_all; filtered_time_hours];
catch
disp(['Skipping file: ', fileName, ' due to an error.']);
end
end
% Initialize variables for S4 data
all_S4_data = struct('time_dt', {}, 'S4', {}, 'Lon_IPP', {}, 'Lat_IPP', {});
% Process S4 data from station files
for station_num = 1:29
filename = sprintf('Station%d.xlsx', station_num);
try
% Read S4 data
data = readmatrix(filename, 'OutputType', 'string');
time_str = data(:, 1); % Time strings
S4 = str2double(data(:, 3)); % S4 values
Lon_IPP = str2double(data(:, 15)); % Longitude
Lat_IPP = str2double(data(:, 16)); % Latitude
elevation = str2double(data(:, 5)); % Elevation angle
% Convert time strings to datetime format
time_dt = datetime(time_str, 'InputFormat', 'yyyy-MM-dd''T''HH:mm:ss.SSS');
% Apply filters
date_filter = (time_dt >= start_time) & (time_dt <= end_time) & (elevation >= 30);
if any(date_filter)
all_S4_data(station_num).time_dt = time_dt(date_filter);
all_S4_data(station_num).S4 = S4(date_filter);
all_S4_data(station_num).Lon_IPP = Lon_IPP(date_filter);
all_S4_data(station_num).Lat_IPP = Lat_IPP(date_filter);
end
catch
disp(['Error processing Station ', num2str(station_num)]);
end
end
% Create figure for plotting
figure('Position', [100, 100, 1200, 800]); % Larger figure
% Loop through each 5-minute interval and create frames
for i = 1:length(time_bins) - 1
% Initialize map for the current time interval
ax = worldmap(latlim, lonlim);
setm(ax, 'FFaceColor', [0.5 0.7 0.9]);
load coastlines;
geoshow(coastlat, coastlon, 'DisplayType', 'polygon', 'FaceColor', [0.6 0.8 0.6]);
% Calculate and display the average S4 value
avg_S4 = [];
for station_num = 1:length(all_S4_data)
if ~isempty(all_S4_data(station_num).time_dt)
current_interval = (all_S4_data(station_num).time_dt >= time_bins(i)) & ...
(all_S4_data(station_num).time_dt < time_bins(i + 1));
if any(current_interval)
avg_S4 = [avg_S4; all_S4_data(station_num).S4(current_interval)];
end
end
end
if ~isempty(avg_S4)
avg_S4_value = mean(avg_S4, 'omitnan');
disp(['Average S4 for time ', datestr(time_bins(i)), ': ', num2str(avg_S4_value)]);
else
avg_S4_value = NaN;
end
% Plot S4 data
for station_num = 1:length(all_S4_data)
if ~isempty(all_S4_data(station_num).time_dt)
current_interval = (all_S4_data(station_num).time_dt >= time_bins(i)) & ...
(all_S4_data(station_num).time_dt < time_bins(i + 1));
if any(current_interval)
scatterm(all_S4_data(station_num).Lat_IPP(current_interval), ...
all_S4_data(station_num).Lon_IPP(current_interval), ...
40, all_S4_data(station_num).S4(current_interval), ...
'filled', 'MarkerEdgeColor', 'k', 'MarkerFaceAlpha', 0.6);
end
end
end
colormap(ax, parula);
caxis([0 0.3]);
c1 = colorbar('Location', 'eastoutside');
c1.Label.String = 'S4';
% Plot ROTI data
idx = filtered_time_hours_all >= (i-1)*5/60 & filtered_time_hours_all < i*5/60;
if any(idx)
scatterm(all_lat(idx), all_lon(idx), ...
10, all_roti(idx), ...
'filled', 'Marker', 's', 'MarkerFaceAlpha', 0.6);
colormap(ax, parula);
caxis([0 0.5]);
c2 = colorbar('Location', 'westoutside');
c2.Label.String = 'ROTI (TECU/min)';
end
% Add title and subtitle
title('Combined S4 and ROTI Data for All Stations');
subtitle(['Time: ', datestr(time_bins(i), 'HH:MM'), ', Average S4: ', num2str(avg_S4_value, '%.3f')]);
% Capture the current frame
frame = getframe(gcf);
writeVideo(v, frame);
end
% Close the video writer
close(v);
disp(['Video saved as ', video_filename]);
Ara
on 4 Dec 2024 at 11:43
here is with different axex but again only show 0.5 for both.
% Create a VideoWriter object for the output video
video_filename = 'S4_and_ROTI_SingleMap_Axes.mp4';
v = VideoWriter(video_filename, 'MPEG-4');
v.FrameRate = 2; % Adjust frame rate
open(v);
% Latitude and longitude limits for Europe
latlim = [20 90];
lonlim = [-30 60];
% Define the time range (5-minute intervals for a full day)
start_time = datetime(2023, 4, 24, 0, 0, 0);
end_time = datetime(2023, 4, 24, 23, 59, 59);
time_bins = start_time:minutes(5):end_time;
% Initialize variables for ROTI data
all_lat = [];
all_lon = [];
all_roti = [];
filtered_time_hours_all = [];
% List of ROTI data files
fileNames = {'Dataajac.txt', 'Dataasky.txt', 'Dataflrs.txt', 'Datafunc.txt', ...
'Datahamd.txt', 'Datahrmn.txt', 'Datalamp.txt', ...
'Datalpal.txt', 'Datalyns.txt', 'Datamdvj.txt', 'Datametg.txt', ...
'Datanklg.txt', 'Dataorid.txt', 'Datapolv.txt', 'Datareyk.txt', ...
'Datariga.txt', 'Datarink.txt', 'Datascor.txt', 'Datasenu.txt', ...
'Datasulp.txt', 'Datatro1.txt'};
% Process ROTI data files
for f = 1:length(fileNames)
fileName = fileNames{f};
try
% Read ROTI data
data = readmatrix(fileName);
time_mjd = data(:, 1); % MJD time
ROTI = data(:, 8); % ROTI values
IPP_lon = data(:, 10); % Longitude
IPP_lat = data(:, 11); % Latitude
% Convert MJD to hours since start of the day
mjd_start = floor(min(time_mjd));
time_hours = (time_mjd - mjd_start) * 24;
% Filter for specific time range (20 to 44 hours)
time_filter = time_hours >= 20 & time_hours < 44;
filtered_time_hours = time_hours(time_filter) - 20;
% Append filtered data
all_lat = [all_lat; IPP_lat(time_filter)];
all_lon = [all_lon; IPP_lon(time_filter)];
all_roti = [all_roti; ROTI(time_filter)];
filtered_time_hours_all = [filtered_time_hours_all; filtered_time_hours];
catch
disp(['Skipping file: ', fileName, ' due to an error.']);
end
end
% Initialize variables for S4 data
all_S4_data = struct('time_dt', {}, 'S4', {}, 'Lon_IPP', {}, 'Lat_IPP', {});
% Process S4 data from station files
for station_num = 1:29
filename = sprintf('Station%d.xlsx', station_num);
try
% Read S4 data
data = readmatrix(filename, 'OutputType', 'string');
time_str = data(:, 1); % Time strings
S4 = str2double(data(:, 3)); % S4 values
Lon_IPP = str2double(data(:, 15)); % Longitude
Lat_IPP = str2double(data(:, 16)); % Latitude
elevation = str2double(data(:, 5)); % Elevation angle
% Convert time strings to datetime format
time_dt = datetime(time_str, 'InputFormat', 'yyyy-MM-dd''T''HH:mm:ss.SSS');
% Apply filters
date_filter = (time_dt >= start_time) & (time_dt <= end_time) & (elevation >= 30);
if any(date_filter)
all_S4_data(station_num).time_dt = time_dt(date_filter);
all_S4_data(station_num).S4 = S4(date_filter);
all_S4_data(station_num).Lon_IPP = Lon_IPP(date_filter);
all_S4_data(station_num).Lat_IPP = Lat_IPP(date_filter);
end
catch
disp(['Error processing Station ', num2str(station_num)]);
end
end
% Create figure for plotting
figure('Position', [100, 100, 1200, 800]); % Larger figure
% Loop through each 5-minute interval and create frames
for i = 1:length(time_bins) - 1
clf; % Clear the figure
% Create world map
ax1 = worldmap(latlim, lonlim);
setm(ax1, 'FFaceColor', [0.5 0.7 0.9]);
load coastlines;
geoshow(coastlat, coastlon, 'DisplayType', 'polygon', 'FaceColor', [0.6 0.8 0.6]);
% Plot S4 data
for station_num = 1:length(all_S4_data)
if ~isempty(all_S4_data(station_num).time_dt)
current_interval = (all_S4_data(station_num).time_dt >= time_bins(i)) & ...
(all_S4_data(station_num).time_dt < time_bins(i + 1));
if any(current_interval)
scatterm(all_S4_data(station_num).Lat_IPP(current_interval), ...
all_S4_data(station_num).Lon_IPP(current_interval), ...
40, all_S4_data(station_num).S4(current_interval), ...
'filled', 'MarkerEdgeColor', 'k', 'MarkerFaceAlpha', 0.6);
end
end
end
% Add colorbar for S4
colormap(ax1, jet);
c1 = colorbar('Location', 'eastoutside');
c1.Label.String = 'S4';
caxis(ax1, [0 0.3]); % Set color axis limits for S4
% Create another map for ROTI
ax2 = worldmap(latlim, lonlim); % Axis for ROTI
setm(ax2, 'FFaceColor', 'none'); % Make the background transparent
load coastlines
geoshow(coastlat, coastlon, 'DisplayType', 'polygon', 'FaceColor', [0.6 0.8 0.6]);
hold on; % Allow overlay of ROTI data
% Plot ROTI data
idx = filtered_time_hours_all >= (i-1)*5/60 & filtered_time_hours_all < i*5/60;
if any(idx)
scatterm(all_lat(idx), all_lon(idx), 10, all_roti(idx), 'filled', 'Marker', 's');
% Add colorbar for ROTI
c2 = colorbar('Location', 'westoutside');
c2.Label.String = 'ROTI (TECU/min)';
caxis(ax2, [0 0.5]); % Set color axis limits for ROTI
end
% Add title
title(['S4 and ROTI Data at ', datestr(time_bins(i), 'HH:MM')]);
% Capture frame for video
frame = getframe(gcf);
writeVideo(v, frame);
end
% Close the video writer
close(v);
disp(['Video saved as ', video_filename]);
See Also
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)