File Exchange

image thumbnail

line_fewer_markers

version 1.5.0.0 (30.2 KB) by Massimo Ciacci
An alias of the LINE command, with additional control for markers, in number and location.

10 Downloads

Updated 17 Oct 2014

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

Allows control of nr of markers, and positioning uniformly along x or along the curve, and with the option to lock on a local max.
Accept same inputs as the LINE command, with the additional parameter NUM_MARKERS, specifying how many markers to be plot.
Inspired by the first answer given by Ioannis Filippidis on
http://www.mathworks.com/matlabcentral/answers/2165-too-many-markers

Cite As

Massimo Ciacci (2020). line_fewer_markers (https://www.mathworks.com/matlabcentral/fileexchange/42560-line_fewer_markers), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (51)

Excellent

Podda

Dani Bechev

Hello. Is it possible to create a logarithmic scaling of the X and Y axes? For X there is 'spacing', 'logx' for Y I didn't find anything? I need a combined plot with logx and logy. Thanks a lot!

So beautiful!

Andre

Nice submission. It would be great if it also accepted datetime as input for x.

DF

Fantastic functions, works great in MATLAB R2014a and R2014b. Thank you!

DF

Fantastic functions, works great in MATLAB R2014a and R2015b. Thank you!

SR Satyal

Saved my day. Great job. I am happy to report that it works like a charm when implemented inside a nested for loop. Also, had different number of data for each lines to plot and the only thing that I needed to work out was the spacing number for each case in order to have somewhat similar looking marker spacing. One data set had 6600 elements and other only 45 and worked like a charm when both were plotted in a same plot. Again thank you.

SR Satyal

Superb! Works like a charm with yyaxis left/right

I want to draw time vs displacement curve. but the showing the is not appropiate. How can I draw the correct figure?

Thank you for your uploading. But I can tak e help from you! When i have run my code, it shows undefined funcction or variable 'line_fewer_markers'. I am using matlabR2015a. How can I solve my problem? I am waiting for your kind help. Thank you.

This is great and I am so appreciated. I have one question; In curves I plot the end of curves should be clear but the marker on it will make them inaccurate. How I can remove the last marker on the curve. Thanks a lot.

Great code! So useful!

Great function! However, I discovered one problem: If I plot x vs y and y is only zeros, the interp1 function encounters a problem. Replacing line 94 with the following solved the problem for me (The result becomes nan and not inf, since one divides 0 by 0):

(sum(isinf(yNrm) | isnan(yNrm))>0) || sum(isinf(x) | isnan(x))>0

Thanks for the attribution. For posterity, the function that I mentioned in that thread is here: https://github.com/johnyf/plot_utils/blob/bd304279ee0e000f72fc936e0ef9f3a690d4e654/plot_subsample/plot_fewer_markers.m

Ha Nguyen

It got this error when I run:
Error using interp1 (line 257)
The values of X should be distinct.

Error in line_fewer_markers (line 101)
ti = round(interp1(s,t,si)); %find x index of markers

XUE BAI

Really a man of genius!!! Thank you! It takes me a long time to find a solution to plot the curves in this way.

Jane

It seems the property+value pair for 'spacing', 'curve' is not working for straight lines but only for curves. But the spacing is actually the same when plotting straight lines anyway. Right?

Thomas

Great tool!
I have a feature suggestion: if there are multiple lines to be plotted, distribute the markers such that they do not overlap.

SandStone

Very nice man! Thank you!!

@Jeril K: put the function in the same folder as the script you want to run it in. When that is done, see: http://blogs.mathworks.com/pick/2013/08/16/line-plots-with-fewer-markers/

SandStone

Very nice man! Thank you!!

Glo

To apply marker on the minimum value replace Line 108:
[Mv,idx] = max(y); to
[Mv,idx] = min(y);

Useful in case it is important to show minimum value, e.g. S-parameter graph.

Jeril K

I am very new to MATLAB, could anyone please tell me how to use 'line_fewer_markers' function. While I am trying to create a chart, it says undefined function 'line_fewer_markers'.
Thanks

Great work!

Julio

works very fine. Thank you so much.

Jérémy

Great !
This makes perfectly what I needed.
Thank you for your answer so fast!

( N.B: For Matlab R2009B, the function iscolumn(x) isn't supported, so I simply replaced it with (size(x,2)==1) )

@Jeremy

Yes, there is a workaround to use it in plotyy, that does not require changing line_fewer_markers, just function pointers, as follows:

figure(10); hold on; set(gca,'FontSize',16); set(gca,'FontName','Times'); set(gcf,'Color',[1,1,1]);
x=(1:10);
y1=x.^2;y2=x.^3;

f1 = @(x,y)line_fewer_markers(x,y,5,'ro','LegendLine','off');
f2 = @(x,y)line_fewer_markers(x,y,8,'bs','Spacing','curve','markerfacecolor','b');
[u,lh1,lh2] = plotyy(x,y1,x,y2,f1,f2)

legend('curve 1','curve 2')

Jérémy

Hello !
Thank you for this usefull function !

Is there a way to use it on a two Y-axis plot, with plotyy ?

Marie

Nice function! I was desperate with my two lines and non-corresponding legends... Great job !

Is that possible to get a 3D version, like plot3 ?

Marie

Just awesome!!!

Nice one!

Xiaoxia Ye

If the function is able to handle nan data as "plot" does, it will be even more awesome.

Pablo

a great function!

Joe

Emerson

Thanks for the update. Love the results!

Jan

Without doubt the results are a strong optical improvement compared to adding a marker at each point.

I just added an option to disable line showing up in the legend (so that only the marker will show up). This option is:'LegendLine','off'

Emerson

I am new to Matlab so I don't feel I'm qualified to give a rating or strong comments yet, but this has been very useful to me in achieving an important plot that seems to be almost inexplicably unnatural to achieve with Matlab.

Massimo, could you tell me whether or not it is possible to show only the markers in the legend rather than the markers in addition to the line style? I ask because my plot is only black and white with the same lines style for every curve, and I imagine this isn't uncommon for publications. The preferred legend for this style shows only the markers.

Updates

1.5.0.0

same as 2 minutes ago

1.4.0.0

- Added support for logarithmic X spacing of markers
- Added example on how to use it with plotyy

1.3.0.0

- added compatibility for column vector input

1.2.0.0

Added option
'LegendLine','off'
to inhibit linestyle to show in legend.

1.1.0.0

- better formatted text indentation
- fixed few typos in comments
- added abbreviations for marker specs (mk,mks,mfc)

MATLAB Release Compatibility
Created with R2011a
Compatible with any release
Platform Compatibility
Windows macOS Linux