Plotting 2 different color maps on one world map

Is it possible to plot 2 different color maps on the worldmap figure? I have a .tif file and a .nc file that are both color scales and I want to overlay them. Currently the code I have will plot them both, but using the same color scale:
figure
hold on
worldmap([69,79], [-167,-117])
colormap('bone');
geoshow([.tif file] A2,R2,DisplayType="surface")
colorbar
colormap('winter')
geoshow([.nc file],,'DisplayType','surface', 'FaceAlpha', 0.2)
colorbar
Is there any way to have 2 different colormaps? Thank you!

3 Commenti

An axes can have one colormap, which is applied to all descendant objects are colormapped.
If you want to have two objects with different colormaps, then you need to overlay two axes.
Umar
Umar il 10 Set 2024
Modificato: Voss il 10 Set 2024
Hi @Judy Wu,
Based on @DGM comments, here is a sample code snippet that demonstrates how to overlay two different colormaps on a world map:
% Load the .tif and .nc files
A2 = imread('your_file.tif'); % Replace with your .tif file path
R2 = maprefpost('your_file.tif'); % Replace with your .tif reference
data_nc = ncread('your_file.nc', 'variable_name'); % Replace with your .nc file and variable
% Create a world map
figure;
worldmap([69, 79], [-167, -117]);
% Create first axes for the .tif file
ax1 = axes('Position', get(gca, 'Position')); % Get current axes position
hold on;
geoshow(ax1, A2, R2, 'DisplayType', 'surface');
colormap(ax1, 'bone'); % Apply first colormap
colorbar(ax1); % Add colorbar for the first dataset
% Create second axes for the .nc file
ax2 = axes('Position', get(gca, 'Position')); % Use the same position
hold on;
geoshow(ax2, data_nc, 'DisplayType', 'surface', 'FaceAlpha', 0.2); % Adjust transparency
colormap(ax2, 'winter'); % Apply second colormap
colorbar(ax2); % Add colorbar for the second dataset
% Set the visibility of the axes
set(ax1, 'Color', 'none'); % Make the background transparent
set(ax2, 'Color', 'none'); % Make the background transparent
When you implement this code, the imread function will load the .tif file, while ncread will load the .nc file. Again, please make sure that you replace the placeholders with your actual file paths and variable names. The axes function is called twice to create two sets of axes that share the same position on the figure and the geoshow function is used to plot each dataset on its respective axes. The FaceAlpha property is set to 0.2 for the second dataset to allow for transparency, making it easier to visualize both datasets simultaneously. Each axes has its own colormap and colorbar, allowing for distinct color representations.
Please let us know if you have any further questions.
Thank you for your replies! I have tested the code and I was able to plot two different color scales! However, it seems to plot the .tif and .nc files on a typical xy axis.
Is it possible for both of the axes to be on this world map projection (see below; worldmap([69, 79], [-167, -117]))?
WorldMap_Template
Thank you in advance!

Accedi per commentare.

 Risposta accettata

Umar
Umar il 12 Set 2024

Hi @Judy Wu,

You asked, “Is it possible for both of the axes to be on this world map projection (see below; worldmap([69, 79], [-167, -117]))?”

Please see my response to your comments below.

To achieve the desired outcome of overlaying both datasets on the specified world map projection, you need to make sure that both axes (ax1 and ax2) are created within the context of the world map coordinate system. Here is a refined version of your code that makes sure both datasets are plotted correctly on the world map projection:

% Load the .tif and .nc files
A2 = imread('your_file.tif'); % Replace with your .tif file path
R2 = maprefpost('your_file.tif'); % Replace with your .tif reference
data_nc = ncread('your_file.nc', 'variable_name'); % Replace with your .nc file    
and variable
% Create a world map
figure;
worldmap([69, 79], [-167, -117]);
% Create first axes for the .tif file
ax1 = axesm('MapProjection', 'mercator', 'Frame', 'on', 'Grid', 'on'); % Define 
map projection
hold on;
geoshow(ax1, A2, R2, 'DisplayType', 'surface');
colormap(ax1, 'bone'); % Apply first colormap
colorbar(ax1); % Add colorbar for the first dataset
% Create second axes for the .nc file
ax2 = axesm('MapProjection', 'mercator', 'Frame', 'on', 'Grid', 'on'); % Same 
projection
hold on;
geoshow(ax2, data_nc, 'DisplayType', 'surface', 'FaceAlpha', 0.2); % Adjust 
transparency
colormap(ax2, 'winter'); % Apply second colormap
colorbar(ax2); % Add colorbar for the second dataset
% Set the visibility of the axes
set(ax1, 'Color', 'none'); % Make the background transparent
set(ax2, 'Color', 'none'); % Make the background transparent
% Ensure both axes are displayed together in the same figure
linkaxes([ax1 ax2]); % Link axes if necessary for synchronized zoom/pan

In the above refined code, the use of axesm allows you to specify a map projection (in this case, mercator) which is essential to make sure that both datasets are aligned correctly on a geographical representation. Using linkaxes helps synchronize zooming and panning between both datasets if required. Also, the FaceAlpha property remains set to allow visibility of overlapping areas between different datasets.

Use compatible coordinate reference systems (CRS) for your .tif and .nc files. Before plotting, I will advise to validate your geospatial data for any inconsistencies or missing values that may affect visualization.

For further guidance and information on the matlab functions mentioned in comments, please refer to

axesm

https://www.mathworks.com/help/map/ref/axesm.html

linkaxes

https://www.mathworks.com/help/matlab/ref/linkaxes.html?s_tid=doc_ta

By following these adjustments and considerations, you should be able to visualize both datasets effectively on a unified world map projection. If you have further questions or need additional assistance, feel free to ask!

5 Commenti

Hi there! Thank you for your response. I was able to plot my files on the projection that I want, but the "axesm" lines don't seem to create a new axis in this case, and I am running into the original issue, where both the files are plotting as one color scale (see example screenshot below). Thank you in advance for any advice and guidance!
It would be a lot easier to troubleshoot if you could attach some sample files (in a zip archive). I don't have a bunch of geolocated data files from which I could build a comparable example. It would be better to just start with what you need.

Hi @Judy Wu,

Thank you for your follow-up! It sounds like you are on the right track with your map plotting but after reading your comments, you are running into issues with color scaling for your datasets. So, to plot multiple datasets with different color scales, you need to set the ColorLimits property for each dataset individually which makes sure that each dataset utilizes its own color scale, allowing for better differentiation. So, when call geoshow, you can specify additional parameters such as ColorLimits. Here is how you can modify and implement code above.

% Load the .tif and .nc files
A2 = imread('your_file.tif'); % Your .tif file path
R2 = maprefpost('your_file.tif'); % Your .tif reference
data_nc = ncread('your_file.nc', 'variable_name'); % Your .nc   file and variable
% Create a world map
figure;
worldmap([69, 79], [-167, -117]);
% Create first axes for the .tif file
ax1 = axesm('MapProjection', 'mercator', 'Frame', 'on', 'Grid',   'on');
hold on;
% Set unique color limits for the first dataset
cLimits1 = [min(A2(:)), max(A2(:))]; % Set limits based on your   data range
geoshow(ax1, A2, R2, 'DisplayType', 'surface', 'ZData', A2);
colormap(ax1, 'bone');
caxis(cLimits1); % Apply color limits
colorbar(ax1);
% Create second axes for the .nc file
ax2 = axesm('MapProjection', 'mercator', 'Frame', 'on', 'Grid',   'on');
hold on;
% Set unique color limits for the second dataset
cLimits2 = [min(data_nc(:)), max(data_nc(:))]; % Set limits based   on your data   
range
geoshow(ax2, data_nc, 'DisplayType', 'surface', 'FaceAlpha',   0.5); % Adjust
transparency if needed
colormap(ax2, 'winter');
caxis(cLimits2); % Apply color limits
colorbar(ax2);
% Set background transparency for both axes
set(ax1, 'Color', 'none');
set(ax2, 'Color', 'none');
% Ensure both axes are displayed together in the same figure
linkaxes([ax1 ax2]); % Link axes if necessary for synchronized   zoom/pan

You might want to adjust the FaceAlpha property for one of the datasets (as shown in the code) to allow overlapping areas to be visible while still being able to distinguish between colors. Before plotting, make that both datasets are valid and do not contain NaN or infinite values that could disrupt visualization. Use functions like isnan() or isfinite() to check your data. If you have any further questions or need additional assistance, feel free to reach out!

Thank you for your help! I was able to plot the figure I wanted by first plotting a new axis before defining it with something like this:
NewAxis = axes('position', get(gca, 'position'), 'Visible', 'off');
ax1 = axesm('MapProjection', 'mercator', 'Frame', 'on', 'Grid', 'on');
Hi @Judy Wu,
Thank you for your reply. I am delighted to hear that you were able to successfully plot the figure you needed by creating a new axis.

Accedi per commentare.

Più risposte (0)

Prodotti

Release

R2021b

Richiesto:

il 10 Set 2024

Commentato:

il 14 Set 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by