Why can't I display a line on a plot (can only have points marker)

10 ビュー (過去 30 日間)
A LL
A LL 2021 年 6 月 28 日
編集済み: dpb 2021 年 6 月 29 日
I'm trying to plot a line (x1,y1) on a plot where I applied a mask for land (green) and seas (blue). Whatever the LineSpec I try to use it only displays marker points instead of a line.
I tried to plot the three different options below (plt1, plt2, plt3). I get (x1,y1) in the first case, but it displays points instead of a continuous line as expected (matlab_fig1.png). In the second (matlab_fig2.png) and third case (matlab_fig3.png), I don't get anything.
Here is my code:
%Plot in cartesian coordinates
%Make Land mask
load([pwd '/Grid/bathyEASE.mat']); % load bathymetry
Land=NaN(361,361);
Land(bathyEASE>0)=1;
Land(bathyEASE<0)=0;
%plot
nexttile
hold on; box on;
xlim([0 361]);
ylim([0 361]);
p=pcolor(Land);
colormap(winter);
set(p,'facealpha',0.5)
shading flat;
axis ij;
x1 = [159:163];
y1 = 77;
plt1 = plot(x1,y1,'-r','LineWidth',3,'Marker','.','MarkerEdgeColor','r'); %Refer to --> matlab_fig.png
%plt2 = plot(x1,y1,'-r','LineWidth',3); %Refer to --> matlab_fig2.png
%plt3 = plot(x1,y1); %Refer to --> matlab_fig3.png
Thank you

採用された回答

dpb
dpb 2021 年 6 月 28 日
編集済み: dpb 2021 年 6 月 29 日
x1 = [159:163];
y1 = 77;
plt1 = plot(x1,y1,'-r','LineWidth',3,'Marker','.','MarkerEdgeColor','r');
When you plot() a vector of one variable against a constant in the other, plot creates a line object for each element of the vector and a single pixel marker simply isn't large enough to show up; there is no line created because there are (in this case 5) separate line objects, each consisting of only a single point.
You also have the sequence of commands in wrong order; or you need a hold on first after setting the axes limits and orientations or plot will cause refreshing the limits of the plot to match the plotted data.
If you want to plot a single vertical or horizontal line; the xline or yline functions are the better way to do so; they're relatively recent but get around the need to duplicate the constant data value manually (but cross the entire axis in extent so not helpful here -- Addendum/Correction --dpb)
Try something more like
xlim([0 361]),ylim([0 361])
axis ij
x=[159:163];
y=77;
hL=plot(x,repmat(y,size(x)),'-r','LineWidth',3); % ammended remove yline call -- dpb
and joy should ensue.
  3 件のコメント
dpb
dpb 2021 年 6 月 28 日
Oh, yeah, I forget (that's what happens with age, it seems, I don't recommend it! :) )
Indeed, x/yline do go from one edge of the axes to the other -- but note that you only need the first and last points to draw a straight line, not all the intermediary ones.
xlim([0 361]),ylim([0 361])
axis ij
hold on
x=[159,163];
y=repmat(77,size(x));
hL=plot(x,y,'-r','LineWidth',3);
A LL
A LL 2021 年 6 月 28 日
Right, this is clever! Thank you dpb :)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by