2016-03-13 52 views
4

我想要實現的是一個經典的彎矩分佈圖可看類似的東西:MATLAB - 在xy曲線下的條紋面積(彎矩分佈)

Example of plot I want to create

我嘗試使用面積, xy和酒吧情節,最後一個是最接近我需要的 - 但它仍然不是我能接受的。我可以使用任意形式的數據。

+0

有一些文件交換的條目都可以實現這個。這不是內置功能。有一篇很棒的博客文章強調了他們,並展示了一些示例[http://blogs.mathworks.com/pick/2011/07/15/creating-hatched-patches/] – Suever

+0

謝謝,我肯定會採取看看這個方法。 –

+1

或者,你可以把它剪掉,先畫出輪廓,然後使用沒有標記的'stem()'來畫線。一個用於積極的位,一個用於消極的位。但是,只有當你是一種特殊的懶惰時:D –

回答

6

雖然Daniel's answer是更通用的,並且可用於傾斜的條紋,這裏是使用stem而沒有標記和基線的簡單解決方案:

x1 = -3; 
x2 = 2; 
upfun = @(x) -1/10*(x-x1).*(x-x2); 
downfun = @(x) 1/5*(x-x1).*(x-x2); 

x_dense = linspace(x1,x2,100); 
x_sparse = linspace(x1,x2,20); 

%// plot outline 
plot(x_dense,upfun(x_dense),'b-',x_dense,downfun(x_dense),'b-'); 
hold on; 
%// plot stripes 
stem(x_sparse,upfun(x_sparse),'b','marker','none','showbaseline','off'); 
stem(x_sparse,downfun(x_sparse),'b','marker','none','showbaseline','off'); 

結果:

result

+0

謝謝,這很好 –

3

我會解決它手動生成你想要的行。

%some example plot 
x1 = -3; 
x2 = 2; 
upfun = @(x) -1/10*(x-x1).*(x-x2); 
downfun = @(x) 1/5*(x-x1).*(x-x2); 
%set slope you want. Inf for vertical lines 
slope=inf; 

x_dense = linspace(x1,x2,100); 
x_sparse = linspace(x1,x2,20); 

%plotting it without the stripes. nan is used not to have unintended lines connecting first and second function 
plot([x_dense ,nan,x_dense ],[upfun(x_dense),nan,downfun(x_dense)]) 

x_stripes=nan(size(x_sparse).*[3,1]); 
y_stripes=nan(size(x_sparse).*[3,1]); 

if slope==inf 
    %vertical lines, no math needed to know the x-value. 
    x_stripes(1,:)=x_sparse; 
    x_stripes(2,:)=x_sparse; 
else 
    %intersect both functions with the sloped stripes to know where they 
    %end 
    for stripe=1:numel(x_sparse) 
     x_ax=x_sparse(stripe); 
     x_stripes(1,stripe)=fzero(@(x)(upfun(x)-slope*(x-x_ax)),x_ax); 
     x_stripes(2,stripe)=fzero(@(x)(downfun(x)-slope*(x-x_ax)),x_ax); 
    end 
end 
y_stripes(1,:)=upfun(x_stripes(1,:)); 
y_stripes(2,:)=downfun(x_stripes(2,:)); 
x_stripes=reshape(x_stripes,1,[]); 
y_stripes=reshape(y_stripes,1,[]); 
plot([x_dense ,nan,x_dense,nan,x_stripes],[upfun(x_dense),nan,downfun(x_dense),nan,y_stripes]) 

實施例爲斜率= 1個

enter image description here

實施例爲斜率= INF

enter image description here

+0

你試過繪製斜條紋嗎?你的手動版本可能會相當容易實現(不像'stem')。 –

+0

這可能是可能的,但需要計算線條之間的交點。特別是對於'g)'這樣的事情,一個條紋的條紋可能會被削減一半。我不使用'stem'的主要原因是將所有數據都保存在一個數據庫中,這樣後續步驟就像添加圖例變得更簡單一樣。 – Daniel

+0

你說得對,我沒有考慮過像'g)這樣的情況。我不會擔心傳說,他們的「行爲」可以被禁用。但我知道這僅僅是一個例子:)我想這也可能取決於OP究竟需要什麼。 –