How do i get evenly spaced marks in my plots?

57 views (last 30 days)
yc j
yc j on 10 Aug 2015
Answered: Mike Garrity on 10 Aug 2015
Hello, I have data with some randomly spaced points.
When I set the plot to line+marks, the marks comes up at the data points, as it is what the marks are supposed to do.
I want the marks to be evenly spaced, no matter what the data points say. In other words, I want the marks to be there to distinguish the lines, just like doted lines, dashed lines, etc.
How can this be done?

Answers (2)

Mike Garrity
Mike Garrity on 10 Aug 2015
I think YC is asking how to place markers evenly on the curve, rather than the kind of fine tuning that AlignVertexCenters does. If that's the case ...
Consider this example:
rng default
x = randn(1,37);
x = sort(x);
y = sin(x);
Those makers show where the data points are, but they don't look very good if you're just using them to identify a particular curve. In that case, we'd like to just have markers spread evenly along the curve.
To do that, we need to use interp1 to find some evenly spaced locations:
range = [min(x), max(x)];
x2 = linspace(range(1),range(2),12);
y2 = interp1(x,y,x2,'linear');
hold on
There are a couple of problems with that. The obvious one is that the markers aren't the same color as the line. The less obvious one comes when we add a legend. There isn't a single line which the legend can point at which has both the line and the marker. I'm afraid that to solve these issues, things get a bit more involved:
h1 = plot(x,y);
hold on
h2 = plot(x2,y2,'+');
h3 = plot(nan,nan,'-+');
set([h2 h3],'Color',h1.Color);
h3.Visible = 'off';
We actually need 3 line objects. The first one draws the line. The second one draws the markers, with different spacing. The third one is invisible, and just exists to tell the legend what we think it should show.


Community Treasure Hunt

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

Start Hunting!