Comments on: Coloring a line based on height, gradient, or some other value in MATLAB https://blogs.mathworks.com/videos/2014/08/12/coloring-a-line-based-on-height-gradient-or-some-other-value-in-matlab/?s_tid=feedtopost Stuart uses video to share his experiences solving problems with MATLAB day-to-day, interesting new features, plus tips and tricks he has picked up along the way. Thu, 02 Oct 2014 18:54:31 +0000 hourly 1 https://wordpress.org/?v=6.2.2 By: Joe https://blogs.mathworks.com/videos/2014/08/12/coloring-a-line-based-on-height-gradient-or-some-other-value-in-matlab/#comment-44272 Thu, 02 Oct 2014 18:54:31 +0000 https://blogs.mathworks.com/videos/?p=1466#comment-44272 Julian,

Thank you. I have looked for something like your surface trick for a long time. That works well, is fast and tolerates NaNs in the Cdata. For a case with N points and N-1 segments between them, I modified it to, e.g., [x(1:(end – 1)) x(2:end)] for Xdata, which also works.

]]>
By: Doug https://blogs.mathworks.com/videos/2014/08/12/coloring-a-line-based-on-height-gradient-or-some-other-value-in-matlab/#comment-42884 Fri, 05 Sep 2014 12:55:15 +0000 https://blogs.mathworks.com/videos/?p=1466#comment-42884 Pekka,

I mostly used that terminology because the people looking for something like this would not be googling for patch object, they would be googling for words in my title. I agree, this does not scale to big sizes well, but for the right situations, this is a decent solution.

Doug

]]>
By: Pekka https://blogs.mathworks.com/videos/2014/08/12/coloring-a-line-based-on-height-gradient-or-some-other-value-in-matlab/#comment-42817 Thu, 04 Sep 2014 20:52:30 +0000 https://blogs.mathworks.com/videos/?p=1466#comment-42817 Coloring a line is more than slightly misleading here. You use scatter, which creates a separate patch object for each data point (until 2014a). That is horrible for anything more than “a few” elements.
I have requested to have CData property for line objects quite a few times since maybe1995. Since it still has not happened I have also used surface to act as a line. color_line in file exchange.

]]>
By: Julian https://blogs.mathworks.com/videos/2014/08/12/coloring-a-line-based-on-height-gradient-or-some-other-value-in-matlab/#comment-41206 Tue, 19 Aug 2014 22:23:50 +0000 https://blogs.mathworks.com/videos/?p=1466#comment-41206 Base MATLAB has always offered colour 3-D plots, based on height, gradient or some other value, out of the box, using the builtin surface, as used in the popular example peaks. For 2-D plots, the only option seems to be scatter which is fine if you want a scatter plot with separate points but no good for a line plot.

Faced with this problem quite some years ago I coded my own solution which used multiple lines for segment of a curve, allowing each line to be a distinct colour. More recently I spotted a very useful trick with this entry “Colored line or scatter plot” on the FEX. surface() will render 2-D lines, as a special case of 3-D, but you must pass into matrix valued data to make it work, so you have to duplicate the vector:

%% Some Data
x = linspace(0, 2*pi)';
y = sin(x);
plot(x, y); % a basic monochrome plot

%% Trick surface into a 2-D plot
surface('XData', [x x],             ... % N.B.  XYZC Data must have at least 2 cols
        'YData', [y y],             ...
        'ZData', zeros(numel(x),2), ...
        'CData', [y y],             ...
        'FaceColor', 'none',        ...
        'EdgeColor', 'interp',      ...
        'Marker', 'none');
]]>
By: BjornG https://blogs.mathworks.com/videos/2014/08/12/coloring-a-line-based-on-height-gradient-or-some-other-value-in-matlab/#comment-41019 Mon, 18 Aug 2014 08:57:37 +0000 https://blogs.mathworks.com/videos/?p=1466#comment-41019 @Brad: In the matlab file exchange there are at least 2 functions that does that for you. To find them look for cline.

]]>
By: Amro https://blogs.mathworks.com/videos/2014/08/12/coloring-a-line-based-on-height-gradient-or-some-other-value-in-matlab/#comment-40572 Thu, 14 Aug 2014 13:31:20 +0000 https://blogs.mathworks.com/videos/?p=1466#comment-40572 Here is a related question on Stack Overflow: http://stackoverflow.com/questions/11855011/plot3-line-color-based-on-value

]]>
By: Doug https://blogs.mathworks.com/videos/2014/08/12/coloring-a-line-based-on-height-gradient-or-some-other-value-in-matlab/#comment-40506 Wed, 13 Aug 2014 13:33:22 +0000 https://blogs.mathworks.com/videos/?p=1466#comment-40506 @Brad,

This is a good question. You would need to make each segment independently. I was actually going to start with that when I saw the question posted but realized this was pre-built. Seems like next week’s topic is now picked!

]]>
By: Brad Stiritz https://blogs.mathworks.com/videos/2014/08/12/coloring-a-line-based-on-height-gradient-or-some-other-value-in-matlab/#comment-40462 Tue, 12 Aug 2014 20:41:13 +0000 https://blogs.mathworks.com/videos/?p=1466#comment-40462 Hi Doug, thanks for this interesting video. In your example, the goal appears to be to color 1-D points. What if we need to color actual 2-D line segments, as implied by your blog post title? Is there a simple way to achieve this, or will we have to construct a loop and manually color each line object? Thanks in advance.

]]>