Plotting 2 different color maps on one world map

1 回表示 (過去 30 日間)
Judy Wu
Judy Wu 2024 年 9 月 10 日
コメント済み: Umar 2024 年 9 月 14 日
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 件のコメント
Umar
Umar 2024 年 9 月 10 日
編集済み: Voss 2024 年 9 月 10 日
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.
Judy Wu
Judy Wu 2024 年 9 月 11 日
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!

サインインしてコメントする。

採用された回答

Umar
Umar 2024 年 9 月 12 日

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 件のコメント
Judy Wu
Judy Wu 2024 年 9 月 13 日
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');
Umar
Umar 2024 年 9 月 14 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLighting, Transparency, and Shading についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by