File Exchange

image thumbnail

Conditionally colored line plot

version 1.1 (3.62 KB) by Matt Tearle
Plots (2D line) graph split into two colors above and below a given threshold value

4 Downloads

Updated 03 Mar 2020

View Version History

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.

Cite As

Matt Tearle (2021). Conditionally colored line plot (https://www.mathworks.com/matlabcentral/fileexchange/30423-conditionally-colored-line-plot), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (11)

Matt Tearle

@Robin Kämpe: Thanks for the feedback. I'm thinking about how to update this function to allow any name-value pairs that plot() allows, but I'm not sure what the right functional design is, so that it's clear that you want the line below the threshold to have a custom color and the line above to have width 2 (or whatever). In the meantime, you can just do the old manual way to adjust the properties - this function creates a bunch of line objects in the current axes. Unfortunately, they're not neatly arranged, so changing them is a bit ugly. Here's some code that should work, but I apologize that it's not the nicest:
>> splitcolorplot(x,y,0.5)
>> ax = gca;
>> below = cellfun(@(y) all(le(y,0.5)),get(ax.Children,'YData'));
>> set(ax.Children(below),'Color',[0.4000 0.7608 0.6471])
>> set(ax.Children(~below),'LineWidth',2)

(I should probably make a simple change so that the function optionally returns the graphics objects as outputs so at least you can access them more easily.)

Robin Kämpe

Thanks a lot Matt!

Noticed it was tricky to make MATLAB split a plot into different colours. A simple command like plot(x(x<=0), 'r', x(x>=0), 'b') separates the values which result in a separate line in the plot with a different colour.
One question, the plot command I wanted to replace with your function was:
plot(X,Y,'Color',[0.4000 0.7608 0.6471],'LineWidth', 2)

I.e. a more specific colour than the ones I can me with letters (e.g. 'r') and a specific line width. Is that possible with the current version of the function? In that case how should define FMT1 and FMT2 since it demands a string and it's not possible to e.g. provide it with a struct with the cells FMT1.LineWidth and FMT1.Colour.

Thanks a bunch for a great function!

Matt Tearle

@Danny: Thanks for the heads-up! I guess this broke in R2019b with changes to the precedence order. I will work on updating it (it's getting old anyway)

Danny Stanzl

Hi there!
Unfortunately I receive the folllowing error;

Error: File: splitcolorplot.m Line: 51 Column: 36
Invalid syntax for calling function 'clr' on the path. Use a valid syntax or explicitly initialize 'clr' to make it a
variable.

shame as it looks just what i need.

Kawan kandili

Thanks!

dasdsa ddasdsa

Ali Amiri

Matt Tearle

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.

Iliana Cardenes

HI!!

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!

Iliana

Barnaby

Jeremy

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

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

Inspired: Conditionally Colored Plot (CCPLOT)

Community Treasure Hunt

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

Start Hunting!