MATLAB Answers

2D Colored Plot

2 views (last 30 days)
Noah Reilly
Noah Reilly on 24 Jul 2021
Answered: KSSV on 24 Jul 2021
Hello, I've created a script which runs for 100 different values of x and produces 3 different values of Y for each X.
Currently I have these lineplotted, but I think that's kinda boring and I would like to present the data differently.
Below is the current plot
I'd like it so that for every X value where one of the lines has a greater value than the other 2 it is colored in a certain color. Kinda like a contour or surface plot, but without a value for Z. I was thinking some sort of contour or heat map, but neither of those sound like they fit what I need. What plot would you suggest, if any, to do this.
For example. Between X values 1 to 14, the entire plot would be red, between 14 and 29 it'd be blue, and past 29 it'd be yellow.
I'd prefer if you pointed me in the right direction instead of just giving me code for this question, I really wanna learn how to do this because it's not something they taught in my coding course.

Answers (1)

KSSV
KSSV on 24 Jul 2021
You can get the indices of the required values and plot them with different colors.
x = 0:.05:2*pi;
y = sin(x);
figure
hold on
plot(x(y>=0),y(y>=0),'r')
plot(x(y<0),y(y<0),'b')

Community Treasure Hunt

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

Start Hunting!