How do we plot a graph with non available values for y without breaking the continuity?
8 ビュー (過去 30 日間)
古いコメントを表示
INTRO: I have an array M as below:
M=
1 4
2 NaN
3 10
4 NaN
5 NaN
6 15
7 NaN
8 17
9 59
10 NaN
GOAL: I want to plot the second versus the first column as follow:
M=load('M.txt');
h=plot(M(:,1),M(:,2));
PROBLEM: Due to the NaN, it does not plot the gaps correctly and breaks the continuity of the graph.
I wonder if someone could suggest me to some command lines that could fix this issue.
I thank you in advance for your help
Emerson
0 件のコメント
採用された回答
Matt Tearle
2012 年 10 月 25 日
You can interpolate to fill the missing values, as Azzi suggests, or simply remove all the NaNs:
idx = ~isnan(M(:,2));
x = M(idx,1);
y = M(idx,2);
plot(x,y,'o-')
If you don't plot markers on the data points, this will look exactly the same as Azzi's solution using interpolation. The difference is whether or not you care about showing which points are actual data values.
1 件のコメント
Grant
2017 年 4 月 11 日
My problem is that I do care about what is a real value. I want to do what is easy in Excel - have a marker at each real value in my lineplot and have the lines interpolate between points
その他の回答 (1 件)
Azzi Abdelmalek
2012 年 10 月 25 日
編集済み: Azzi Abdelmalek
2012 年 10 月 25 日
Use interpolation
x=M(:,1);
y=M(:,2);
idx=find(~isnan(y))
y=interp1(x(idx),y(idx),x,'linear')
or remove rows containing nan
x=M(:,1);
y=M(:,2);
x(find(isnan(y)))=[]
y(find(isnan(y)))=[]
2 件のコメント
Matt Tearle
2012 年 10 月 25 日
idx = ~isnan(y);
No need for the find. (Sorry, I love logical indexing!)
参考
カテゴリ
Help Center および File Exchange で Line Plots についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!