# Line plots with fewer markers 2

Posted by **Jiro Doke**,

Jiro's pick this week is `line_fewer_markers` by Massimo Ciacci.

"How can I set the intervals of markers in a plot?"

This is a question that I've seen at various times, both in MATLAB Answers and in MATLAB Newsgroup. The question is in regards to the fact that when MATLAB puts markers on a line plot, it puts them on every data point. For plots with many data points, it becomes visually crowded.

```
t = 0:0.005:pi;
plot(t, sin(3*t).*cos(t/2), 'p-')
```

`line_fewer_markers` allows you to specify the number of markers to show, regardless of how many data points are plotted.

```
clf;
line_fewer_markers(t, sin(3*t).*cos(t/2), 10, 'p-');
```

It has some additional options, such as controlling how the spacing is calculated. This is useful especially when the line is curvey. In such cases, you may want to compute the marker spacing based on the distance on the curve, rather than based on the x distance.

clf; line_fewer_markers(t, sin(3*t).*cos(t/2), 10, 'p-', 'Spacing', 'curve');

And finally, the function can return a handle that can be used specifically for legend, so that it displays the correct marker and line style.

clf; h1 = line_fewer_markers(t, sin(3*t).*cos(t/2), 10, 'p-', 'Spacing', 'curve'); h2 = line_fewer_markers(t, -sin(t/2).*cos(3*t), 10, 'o--r', 'Spacing', 'curve', ... 'MarkerSize', 6, 'MarkerFaceColor', 'r'); legend([h1, h2], 'Line 1', 'Line 2', 'Location', 'SouthEast');

Massimo's function is well-documented, and he also includes a few example scripts to show the different use cases.

**Comments**

Let us know what you think here or leave a comment for Massimo.

Get the MATLAB code

Published with MATLAB® R2013a

**Category:**- Picks

### Note

Comments are closed.

## 2 CommentsOldest to Newest

**1**of 2

I’m disappointed with the choice for pick of the week. If user wants to have fewer markers, he can give two plot commands, one for plotting a line using all data points and other, plotting limited no. of data points (depends of user) as markers. Suppose x,y comprise large set of data points,

plot(x,y,’-‘,x(1:n/10:n),y(1:n/10,n),’*’)

**2**of 2

@Viswanath,

Your solution is what this Pick of the Week entry is doing more or less. The author of the entry made it a convenient function so that people do not have to do the double plotting manually. Also, as you can see from the example, he has additional options to better choose the interval (evenly spaced across x-axis or evenly spaced along the curve). He also has a simple interface that returns the appropriate handle for legend creation (see the last example in my post).

## Recent Comments