Why can't I display a line on a plot (can only have points marker)
10 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
dpb
on 28 Jun 2021
Edited: dpb
on 29 Jun 2021
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 Comments
dpb
on 28 Jun 2021
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);
More Answers (0)
See Also
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!