How do you change the color of a selected area under a curve
조회 수: 11 (최근 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
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
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));
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Performance에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!