# define different color for area

36 visualizzazioni (ultimi 30 giorni)
Gianluca De Monaco il 17 Nov 2021
Risposto: Kelly Kearney il 17 Nov 2021
Having diagramed the T function, I would like to color the upper triangle red and the lower one blue, without necessarily having to create two distinct areas. It's possible?
n = 10;
Le = 10000;
xn = linspace(0,Le,n+1);
T = 89250-357*xn/20;
taglio = area(xn,T);
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Kelly Kearney il 17 Nov 2021
I don't think an area plot allows for multiple colors within a single area object. You could accomplish the color change using a patch object instead, but then you need to manually add the appropriate baseline:
n = 10;
Le = 10000;
xn = linspace(0,Le,n+1);
T = 89250-357*xn/20;
xp = [xn xn(end) xn(1) xn(1)];
yp = [T 0 0 T(1)];
cp = sign(yp);
hp = patch(xp, yp, cp);
set(gca, 'clim', [-1 1], 'colormap', [1 0 0; 0 0 1]);
I think it would be more straightforward to just use two area objects instead:
T1 = max(T, 0);
T2 = min(T, 0);
taglio1 = area(xn, T1, 'facecolor', 'b');
hold on;
taglio2 = area(xn, T2, 'facecolor', 'r');
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Più risposte (2)

Image Analyst il 17 Nov 2021
You can use the FaceColor optio:
n = 10;
Le = 10000;
xn = linspace(0,Le,n+1);
T = 89250-357*xn/20;
taglio = area(xn,T, 'FaceColor', 'r');
You can use a 3 element RGB value instead of a letter if you want a custom color, like
myColor = [.5, .3, .2];
taglio = area(xn,T, 'FaceColor', myColor);
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Gianluca De Monaco il 17 Nov 2021
I know the FaceColor property! but I get this result:
I want something like that:
Is there an option that allows you to give a different color as long as the T function is greater than a fixed value? Let me explain ... As long as the T> 0, it is colored blue, while if the T <0, it is colored red.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Blue in Help Center e File Exchange

R2019b

### Community Treasure Hunt

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

Start Hunting!

Translated by