How do you change the color of a selected area under a curve

11 visualizaciones (últimos 30 días)
Aaron Friedman
Aaron Friedman el 11 de Dic. de 2012
I want to change the color of the selected area to say black. Ultimately, I want to have several areas filled with different colors. I know Matlab has specific built-in functions for normal curves, but I want to use my own for learning. Here is some code:
function [P] = N(u,sd,L1,L2)
y1=NC(u,sd,L1);
y2=NC(u,sd,L2);
x=[u-(sd*5):.01:u+(sd*5)];
xL=(L1:.1:L2);
Px = (1/(sqrt(2*pi)*sd)).*exp((-(x-u).^2)./(2*sd.^2));
PxL = (1/(sqrt(2*pi)*sd)).*exp((-(xL-u).^2)./(2*sd.^2));
hold on;
plot(x,Px,'k','LineWidth',4);
area(xL,PxL); %**** THIS IS THE COLOR I WANT TO CHANGE *****
title('Normal Plot - P(x) vs. x');
xlabel('x'); ylabel('P(x)');
axis([u-(sd*5),u+(sd*5),0,max(Px)]);
line([L1,L1],[0,y1],'color','r','LineWidth',2);
line([L2,L2],[0,y2],'color','r','LineWidth',2);
hold off;
function [y]=NC(u, sd, L)
y=(1/(sqrt(2*pi)*sd)).*exp((-(L-u).^2)./(2*sd.^2));
Thanks
  1 comentario
Jonathan Epperl
Jonathan Epperl el 11 de Dic. de 2012
What is wrong with area? See http://www.mathworks.com/help/matlab/ref/area.html and in particular the example on the very bottom.
Alternatively you could use the lower-level function patch.

Iniciar sesión para comentar.

Respuestas (1)

Babak
Babak el 11 de Dic. de 2012
Here it is: Note that I only changed your plot() function to fill() and I added the end point and the first point of your data to the data again. With this, the curve will be closed! and fill would fill color int he closed curve... hope it helps you and answers your question.
function [P] = N(u,sd,L1,L2)
y1=NC(u,sd,L1);
y2=NC(u,sd,L2);
x=[u-(sd*5):.01:u+(sd*5)];
xL=(L1:.1:L2);
Px = (1/(sqrt(2*pi)*sd)).*exp((-(x-u).^2)./(2*sd.^2));
PxL = (1/(sqrt(2*pi)*sd)).*exp((-(xL-u).^2)./(2*sd.^2));
hold on;
fill([x , x(end), x(1)],[Px, Px(end),Px(1)],'k'); %,'LineWidth',4);
%area(xL,PxL); %**** THIS IS THE COLOR I WANT TO CHANGE *****
title('Normal Plot - P(x) vs. x');
xlabel('x'); ylabel('P(x)');
axis([u-(sd*5),u+(sd*5),0,max(Px)]);
line([L1,L1],[0,y1],'color','r','LineWidth',2);
line([L2,L2],[0,y2],'color','r','LineWidth',2);
hold off;
function [y]=NC(u, sd, L)
y=(1/(sqrt(2*pi)*sd)).*exp((-(L-u).^2)./(2*sd.^2));

Categorías

Más información sobre Graphics Performance en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by