![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/336337/image.png)
x-y plot: how to shade the difference between two lines in multiple colors
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
MS
el 23 de Jul. de 2020
Comentada: Star Strider
el 24 de Jul. de 2020
kindly help me to fill the diffrence with two diffrent colur in a x y plot as shown in the below example figure?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/336280/image.png)
I need to shade postive(red color) and negative diffrences(blue color) in diffrent colors. Kindly give your suggesttions
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/336328/image.png)
t= 0:.01:1
AA = cos(t)
BB= sin(t)
diff = AA-BB
plot(t,AA)
hold on
plot(t,BB)
patch([t fliplr(t)], [AA fliplr(BB)], 'r')
hold off
%%% any suggestions Please!!
0 comentarios
Respuesta aceptada
Star Strider
el 24 de Jul. de 2020
Correct idea, not quite the correct approach.
Try this:
t= 0:.01:1;
AA = cos(t);
BB= sin(t);
Lv = AA > BB; % Logical Vector
figure
plot(t,AA)
hold on
plot(t,BB)
patch([t(Lv) fliplr(t(Lv))], [AA(Lv) fliplr(BB(Lv))], 'r')
patch([t(~Lv) fliplr(t(~Lv))], [AA(~Lv) fliplr(BB(~Lv))], 'g')
hold off
producing:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/336337/image.png)
.This uses ‘logical indexing’ to separate the two regions.
The independent variable (‘t’ here) may need greater resolution for more complicated curves for this approach to work correctly. If the curves are data, use linspace to create the indpendent variable vector with greater resolution, and interp1 to interpolate the data to it.
4 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Surface and Mesh Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!