How to add an image on top of geobasemap?

52 views (last 30 days)
Sim
Sim on 11 Dec 2025 at 14:35
Edited: Umar about 22 hours ago
I tried to add an image on top of a geobasemap using
[imgData, ~, alphaData] = imread('Basketball.png');
lat_min = 40;
lat_max = 50;
lon_min = 0;
lon_max = 10;
figure;
ax = geoaxes;
geobasemap(ax, 'grayland');
geoshow(imgData, ...
'XData', [lon_min, lon_max], ... % Longitude bounds
'YData', [lat_min, lat_max], ... % Latitude bounds
'DisplayType', 'texturemap', ... % Display a raster image
'Parent', ax);
Error using designateAxesArgAsParentArg (line 66)
Plotting over a GeographicAxes using the geoshow function is not supported.

Error in geoshow (line 233)
varargin = designateAxesArgAsParentArg(mfilename, varargin{:});
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
but it didn’t work. How can I do this? I read the section "Create Common Plots over Basemap Images", but it doesn’t mention "geobasemap".

Answers (1)

Umar
Umar on 12 Dec 2025 at 9:43
Edited: Umar on 12 Dec 2025 at 10:29

@Sim, I see what happened - in your case you do have the Mapping Toolbox (your geobasemap is working), but the original code syntax was incorrect. Let me give you both solutions.

Solution 1: WITH Mapping Toolbox (Your Case)

Based on the MathWorks documentation for geoshow, here's the correct syntax:

% Read the image
[imgData, ~, alphaData] = imread('/MATLAB Drive/IMG_6894.PNG'); 
%replace it with your directory path
% Define the geographic bounds
latlim = [40, 50];
lonlim = [0, 10];
% Create figure with geoaxes
figure;
gx = geoaxes;
geobasemap(gx, 'grayland');
hold on;
% Correct geoshow syntax for geographic axes
geoshow(imgData, spatialref, 'DisplayType', 'texturemap');
% OR the simpler form:
h = geoshow(latlim, lonlim, imgData);
% Apply transparency
if ~isempty(alphaData)
  set(h, 'AlphaData', alphaData);
end

Why the original failed: According to the documentation, when using geoshow with GeographicAxes (created by geobasemap), the 'Parent' parameter syntax shown in that forum post is not supported. The documentation states: " geoshow is not recommended when used with geographic axes (created by geoaxes or geobasemap). Use geolimits instead to set limits."

Solution 2: WITHOUT Mapping Toolbox (Fallback)

This is what I implemented - works with base MATLAB only:

close all;clear all;clc
% Read the image
[imgData, ~, alphaData] = imread('/MATLAB Drive/IMG_6894.PNG');
% Define the geographic bounds for the image
latlim = [40, 50];
lonlim = [0, 10];
% Create figure
figure('Color', 'w');
ax = axes;
% Display the image with geographic coordinates
imagesc(lonlim, latlim, imgData);
set(ax, 'YDir', 'normal');  % Correct the y-axis direction
axis equal tight;
% Set labels
xlabel('Longitude');
ylabel('Latitude');
title('Basketball Overlay');
% Apply transparency if alphaData exists
if ~isempty(alphaData)
  alpha(alphaData);
end
% Add gridlines for reference
grid on;

Output:

Since you have the Mapping Toolbox, use Solution 1 for the proper geobasemap overlay!

Let me know how it goes.

  9 Comments
Dyuman Joshi
Dyuman Joshi 26 minutes ago
The corrected code doesn't work - see below.
"Any suggestions, please let us know."
My immediate suggestion would be to run your code, at the very least here on the Live editor, before submitting.
As for my suggetion for solving the problem, I'll need to check some things.
% read the PNG and its alpha (transparency)
[imgData, ~, alphaData] = imread('Basketball.png');
% define the geographic bounds
latlim = [40, 50];
lonlim = [0, 10];
% create the spatial reference for the image
R = georefcells(latlim, lonlim, size(imgData));
figure;
gx = geoaxes; % geographic axes
geobasemap(gx,'grayland'); % set the basemap
hold on;
% overlay the image correctly using the spatial reference
h = geoshow(gx, imgData, R, 'DisplayType','texturemap');
Error using geoshow
Expected input number 1, AX, to be one of these types:

matlab.graphics.axis.Axes

Instead its type was matlab.graphics.axis.GeographicAxes.

Error in designateAxesArgAsParentArg (line 47)
validateattributes(ax, {'matlab.graphics.axis.Axes'}, ...
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in geoshow (line 233)
varargin = designateAxesArgAsParentArg(mfilename, varargin{:});
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
% apply transparency if the image has an alpha channel
if ~isempty(alphaData)
set(h, 'AlphaData', alphaData);
end
Umar
Umar about 2 hours ago
Edited: Umar less than a minute ago

@Sim and @Dyuman Joshi,

After reviewing the error messages and documentation, I now understand the root cause of all the failures.so, why all previous attempts failed:

The error message is explicit:

Expected input number 1, AX, to be one of these types:
matlab.graphics.axis.Axes
Instead its type was matlab.graphics.axis.GeographicAxes.

This reveals a fundamental incompatibility: geoshow cannot accept GeographicAxes objects (created by geobasemap/geoaxes), even though GeographicAxes is technically a subclass of Axes. The function explicitly validates and rejects this type.MATLAB has two separate mapping systems:

  • Old system: worldmap/axesm creates matlab.graphics.axis.Axes which works with geoshow
  • New system: geobasemap/geoaxes creates matlab.graphics.axis.GeographicAxes does not work with geoshow

Since you're using geobasemap, every attempt to use geoshow will fail with this exact error, regardless of how the spatial reference is created. After digging through mathworks documentation, I came across addCustomBasemap, so after going through documentation, addCustomBasemap to create a custom basemap from your image, then display it with geobasemap, hope this should work,

[imgData, ~, alphaData] = imread('Basketball.png');
latlim = [40, 50];
lonlim = [0, 10];
% Create spatial reference for the image
R = georefcells(latlim, lonlim, size(imgData));
% Add image as a custom basemap
addCustomBasemap('basketball', imgData, R);
% Display using geobasemap
figure;
geobasemap('basketball');

Reference:

https://www.mathworks.com/help/map/ref/addcustombasemap.html#mw_d2d491dc-ba64-486c-b4b4-6128211d3037

@Dyuman Joshi - need your cooperation, could you please test this approach to confirm it works? Also, I would like your help with possible solutions or work around to help out @Sim.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!