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

11 ビュー (過去 30 日間)
Aaron Friedman
Aaron Friedman 2012 年 12 月 11 日
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 Center および File ExchangeGraphics Performance についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by