2016-08-01 139 views
1

我在Matlab中使用y = normpdf(x,mu,sigma);繪製正常曲線。 這爲我畫了一條正常的曲線。但是,我需要在曲線上顯示其他信息,例如曲線上有豎線以顯示mu和sigma。與此類似: enter image description hereMatlab:如何在正常曲線上顯示西格瑪和mu

是否有Matlab函數在曲線上繪製這樣的垂直線?

感謝, 阿依

+1

只有'plot'-ting 3行(或使用'line')有什麼問題嗎? –

+0

它的工作原理和繪製線。但是,我需要將sigma和mu寫在垂直線相交的x軸上。 –

回答

5

這有沒有內置的功能,但我們可以通過手輕鬆地做到這一點:

建立正常的曲線,繪製它:

x = -2:0.05:2; 
mu = 0; sigma = 0.5; 
y = normpdf(x,mu,sigma); 
plot(x,y) 

添加線西格瑪:

hold on; 
plot([mu - sigma mu - sigma],[0 max(y)],'--') 
plot([mu + sigma mu + sigma],[0 max(y)],'--') 

你可以把它改成任何小號igma你需要(2sigma 3sigma)。 如何添加文本?通過這種方式:

text(0.1,-0.05,'mu + sigma'); 

,或者如果你想讓它看起來很漂亮:

text(-0.65,-0.05,'\mu - \sigma') 

結果:

enter image description here

0

繪製在Matlab線最簡單的方法可能是使用簡單plot 。下面的代碼段吸引了來自[X1,Y1]一行[x2,y2]:

plot([x1,y1], [x2,y2], plotoptions) 

爲了確保這條線也是對你目前的數字使用hold on可見。這導致類似下面的代碼:

y = normpdf(x,mu,sigma) 
figure(1) 
    hold on 
    plot(x,y) % Your normal distribution 
    plot([mu,startY], [mu, stopY], plotoptions) % Plot the line at position mu. 
               % The startY and stopY indicate the bottom and top of your graph. 
    hold off 

Mathworks公司文檔情節的頁面可以是一個很好的參考,有很多的例子,不同的劇情選項。例如如何製作虛線或彩色線: http://www.mathworks.com/help/matlab/ref/plot.html?searchHighlight=plot%20line