Using an geographic plot as a background for other matlab plots

42 Ansichten (letzte 30 Tage)
Charan
Charan am 18 Aug. 2023
Kommentiert: Charan am 4 Sep. 2023
HI,
I have an geographic plot with two latitude and Longitude information, I am able to get the actual plot as expected.
f = figure()
f =
Figure (1) with properties: Number: 1 Name: '' Color: [1 1 1] Position: [671 558 577 433] Units: 'pixels' Show all properties
lat = [48.2402500000000 48.285059599999980];
lon = [16.2759199000000 16.179709699999990];
geoplot(lat,lon,'-o');
Can Anyone please help me out how can i save the above image and use as an background for other matlab plot.
When i tried using the command, "I = imread('test1.png');" i was able to save it but in further i was not bale to add it as a background to other plots.
Expecting for the answer thank you.

Antworten (1)

Narvik
Narvik am 31 Aug. 2023
Hi,
You can accomplish this by creating the required MATLAB plot and then plotting the saved background image using the image function. Using the "uistack" function, the image can be placed in the plot's background.
Please find the working code below
% geoplot
f = figure();
lat = [48.2402500000000 48.285059599999980];
lon = [16.2759199000000 16.179709699999990];
gp = geoplot(lat,lon,'-o');
% removing labels and ticks
% for a clean background image
gx = gp.Parent;
gx.LatitudeLabel.String = '';
gx.LongitudeLabel.String = '';
gx.LatitudeAxis.TickLabels = {};
gx.LongitudeAxis.TickLabels = {};
% exporting the plot as a png
exportgraphics(f, 'geoplot.png');
% plotting a line
x = 0:pi/100:2*pi;
y = sin(x);
plot(x, y, 'r');
axis tight;
% setting the background image
hold on;
% loading the image
I = imread('geoplot.png');
% plotting the image with the
% x, y coordinates of the image's
% boundaries as the current axis' limits
% flipping ylim as some images may
% be flipped in the vertical direction
h = image(xlim,flip(ylim),I);
% sending the image to bottom of stack
uistack(h,'bottom');
The output of the code is as follows:
For more information on the “image” and “uistack” functions, you can refer to the following documentation:
Hope this helps!

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by