Adding fading Background color (green --> yellow --> red) to plot(x,y)

10 vues (au cours des 30 derniers jours)
Daniel Leibundgut
Daniel Leibundgut le 24 Mar 2022
Commenté : Mathieu NOE le 24 Mar 2022
I would like to add a fading colors into the backoground when using plot(x,y).
starting with green at 1.0 and below, fading to yellow (1.2) and yellow fading to red (1.8 and higher). This color schema shall be appllied in both directions x and y.
The following code does only color fading from green to red but noth using yellow:
xdata = [1.378 1.398 1.467 1.558 1.393 1.277 1.775 1.327];
ydata = [1.350 1.660 1.477 1.615 1.471 1.350 1.959 1.400];
figure(11); clf;
plot(xdata,ydata,'o'); grid;
axis square;
axis([1 2 1 2]);
% only two colors green --> red
hold on;
p = patch([1.00 2.00 2.00 1.00],[1.00 1.00 2.00 2.00],...
[1 0.63 0.48],'FaceVertexCData',[0 1 0; 1 0 0; 1 0 0; 1 0 0],...
'FaceColor','interp','EdgeColor','none','FaceAlpha',0.5); % light red
hold off;
Thank you for your help!

Réponse acceptée

Mathieu NOE
Mathieu NOE le 24 Mar 2022
hello daniel
try this and tune the pos_yellow parameter to get exactly the visual aspect yu want
xdata = [1.378 1.398 1.467 1.558 1.393 1.277 1.775 1.327];
ydata = [1.350 1.660 1.477 1.615 1.471 1.350 1.959 1.400];
figure(11); clf;
plot(xdata,ydata,'o'); grid;
axis square;
axis([1 2 1 2]);
% 3 colors green --> yellow --> red
hold on;
pos_yellow = 1.4; % try between 1.2 and 1.5 to match visually your expectations
p = patch([1 pos_yellow 2 2 1 1],[1 1 1 2 2 pos_yellow],[1 0.63 0.48],...
'FaceVertexCData',[0 1 0; 1 1 0; 1 0 0; 1 0 0; 1 0 0; 1 1 0],...
'FaceColor','interp','EdgeColor','none','FaceAlpha',0.5); % light red
hold off;
  4 commentaires
Daniel Leibundgut
Daniel Leibundgut le 24 Mar 2022
great, thank you!
Mathieu NOE
Mathieu NOE le 24 Mar 2022
My pleasure !

Connectez-vous pour commenter.

Plus de réponses (1)

Chunru
Chunru le 24 Mar 2022
xdata = [1.378 1.398 1.467 1.558 1.393 1.277 1.775 1.327];
ydata = [1.350 1.660 1.477 1.615 1.471 1.350 1.959 1.400];
figure(11); clf;
plot(xdata,ydata,'o'); grid;
axis square;
axis([1 2 1 2]);
% only two colors green --> red
hold on;
% Adjust the vertex color
% [0 1 0; 1 0 0; 1 1 0; 1 0 0] => G R Y R for the 4 corners
p = patch([1.00 2.00 2.00 1.00],[1.00 1.00 2.00 2.00],...
[1 0.63 0.48],'FaceVertexCData',[0 1 0; 1 0 0; 1 1 0; 1 0 0],...
'FaceColor','interp','EdgeColor','none','FaceAlpha',0.5); % light red
hold off;

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by