Linking line plot and map (geoshow) together MATLAB

11 次查看(过去 30 天)
Hello all:
I wish to combine the MATLAB spatial plot generated through mapping function and the line plot on the same axis scale. However, the linkprop function is not working in this case. Please suggest how to make both axis similar?
Here is my code:
figure(1);
AX1 = subplot(1,2,1);
ax=axesm('mercator','MapLatLimit',[-65 65],'MapLonLimit',[-180 180]);
f=worldmap([-60 80],[-180 180]);
h = geoshow(coast1, 'DisplayType', 'polygon','facecolor','w');
oceanColor = [.7 .8 .9];%[.5 .7 .9];
setm(ax,'FFaceColor',oceanColor);
setm(ax, 'meridianlabel', 'on', 'parallellabel', 'on');
setm(gca,'mlabelparallel',-90);
hold on;
%Define coordinates for tropical lines
yvec = -180:180;
xvec = ones(size(yvec));
geoshow(23.5*xvec,yvec,'DisplayType','Line','LineWidth',1,'LineStyle','--','Color','k'); hold on;
geoshow(-23.5*xvec,yvec,'DisplayType','Line','LineWidth',1,'LineStyle','--','Color','k'); hold on;
geoshow(35*xvec,yvec,'DisplayType','Line','LineWidth',0.8,'LineStyle','--','Color','r'); hold on;
geoshow(-35*xvec,yvec,'DisplayType','Line','LineWidth',0.8,'LineStyle','--','Color','r'); hold on;
AX2 = subplot(1,2,2);
h1 = plot(B1,X_dist(:,2),'r--','linew',0.8); hold on;
h2 = plot(B,X_dist(:,2),'r-','linew',1.8); hold on;
h3 = plot(B2,X_dist(:,2),'r--','linew',2); hold on;
set(gca,'fontsize',14,'fontname','arial');
degreetick 'y';
linkprop(AX1,'YLim');
linkprop(AX2,'YLim');
However, I am unable to adjust the size and Y-labels of both subplots. The latitudes of both subplots should match.

采纳的回答

Austin M. Weber
Austin M. Weber 2024-6-8
Assuming that I am understanding correctly, if the y-axis of your second subplot is supposed to represent latitude values, then you can try plotting the data using the same map axes specifications as the first subplot. Although, depending on the "x" values in your second subplot you may need to normalize the values so that they fit along the axis as if they were longitude coordinates. Hopefully that makes sense. Here is an example:
load coastlines
figure(1)
subplot(1,2,1)
ax1=axesm('mercator','MapLatLimit',[-65 65]);
tightmap, framem
fillm(coastlat,coastlon,'w','EdgeColor','none')
plabel on
hold on
% Add tropical lines
lonvec = -180:1:180;
latvec = ones(size(lonvec));
plotm( 23.5 .* latvec, lonvec, 'LineW',1.0,'LineS','--','Color','k')
plotm(-23.5 .* latvec, lonvec, 'LineW',1.0,'LineS','--','Color','k')
plotm( 35.0 .* latvec, lonvec, 'LineW',0.8,'LineS','--','Color','r')
plotm(-35.0 .* latvec, lonvec, 'LineW',0.8,'LineS','--','Color','r')
hold off
set(gca,'Color','#03719c') % Sets background color to ocean blue
subplot(1,2,2) % Use the map axes specifications as the previous subplot
ax2=axesm('mercator','MapLatLimit',[-65 65]);
tightmap, framem
B1 = normalize(rand(size(lonvec)),'Range',[-180 180]);
X_dist = normalize(rand(size(B1)),'Range',[-45 45]);
% Sort the data so that the longitudes are sequential
[B1,idx] = sort(B1,'ascend');
X_dist = X_dist(idx);
% Plot your line
plotm(X_dist,B1)
plabel on
As you can see, the y-axis of the second subplot is now aligned with that of the first subplot.
  2 个评论
Poulomi Ganguli
Poulomi Ganguli 2024-6-10
for subplot(1,2,2), I have latitude values and linear values, so plotm is not working. Here I attach the values as test1 excel sheet. In the code, you have both axes shown as lat-long but in my case, y is lat and x is linear. Can we scale value directly like this?
Austin M. Weber
Austin M. Weber 2024-6-10
The plotm function does work, but the latitude data needs to be sorted so that it is in sequential order. However, you are correct that the function assumes the "values" are in units of longitudinal degrees, and unfortunately that means when you add tick labels the values will have degree symbols next to them, which you don't want (although the values themselves will be correct).
In the code block below, I tried adding my own x-tick labels using textm, but there is probably a better way to go about this. If I think of another solution I will let you know.
% Import data from the Excel file
data = readtable('test1.xlsx','FileType','Spreadsheet');
lat = data.Latitude;
% Sort the latitudes
[lat,idx] = sort(lat,'descend');
val = data.Value;
% Sort the corresponding values
val = val(idx);
% Plot the data
figure
ax2=axesm('mercator','MapLatLimit',[-65 65],'MapLonLimit',[0 40]);
tightmap, framem
plotm(lat, val, '.-',...
'MarkerEdgeColor','#03719c','MarkerSize',8,...
'LineWidth', 1, 'Color', '#04a7e7')
xlabel('Value')
hold on
% Add custom x-tick labels
textm(-63.5, 1, '0', 'HorizontalAlignment','left')
textm(-63.5, 20, '20', 'HorizontalAlignment','center')
textm(-63.5, 39, '40', 'HorizontalAlignment','right')
hold off
plabel on % Latitude tick labels

请先登录,再进行评论。

更多回答(0 个)

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by