File Exchange

image thumbnail

Conditionally colored line plot

version (3.76 KB) by

Plots (2D line) graph split into two colors above and below a given threshold value



View License

Plots vectors x & y using one color for when y is greater than a given threshold value, and another color when y is less than the threshold.
You can optionally specify any standard formatting string that PLOT accepts (eg 'rx:' for a red dotted line with cross markers).

The plotting follows the standard MATLAB PLOT approach of linearly interpolating between data values. The coloring/linestyle changes at y = threshold, even on the interpolated line.

Comments and Ratings (5)

Ali Amiri

Matt Tearle

Matt Tearle (view profile)

Hi Iliana, Thanks for the feedback. Unfortunately at the moment the function can't handle either of those cases. For multiple columns, you'd just have to hold on and loop over the columns. To do a vertical threshold I guess you could do it horizontally then switch all the x and y data:
>> ax = gca;
>> arrayfun(@(l) set(l,'XData',l.YData,'YData',l.XData),ax.Children)

I'll keep these ideas in mind for future improvements.


Thank you for this function! :)

Is there a way to apply it when Y has more than one vector in it? So when every line exceeds a threshold then it changes the colour?

Another question, is there a way for the threshold to apply to the index in X and not the values in Y? So the threshold would appear vertically, not horizontally?

Thank you so much!



Barnaby (view profile)


Jeremy (view profile)

  • 1 file
  • 4.87097

It works well as described, but could be improved by handling multiple thresholds.


Updated license

MATLAB Release
MATLAB 7.11 (R2010b)

Inspired: Conditionally Colored Plot (CCPLOT)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video