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

6 次查看(过去 30 天)
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 个评论
Jonathan Epperl
Jonathan Epperl 2012-12-11
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.

请先登录,再进行评论。

回答(1 个)

Babak
Babak 2012-12-11
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));

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by