Avoiding zeros doesnt wrok while plotiing from a data file.
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
x=load(sprintf('test_line.dat'))
x1=x(:,1);
x1r=x(:,2);
% x1(x1r==0)=Nan %This shows erorr
x1r(x1r==0)=NaN;
x10=plot(x1,x1r,--b','LineWidth', 1.5);
%x20=plot(x1,x1r,.g','LineWidth', 1.5);
hold on
As long its line plot, it doenst work and connect next point with a line. If I use x1(x1r==0)=NAN, it still shows line connecting a gap. Using 'dot'as '.g' in the coommnet works fine, but if I want to plot many such plots overlapping, that will show only one color. Any sugegstions please?. Line plots would be betetr, if i can plot. Thanks...
Edit: Thes size of x1 or x1r =1450088 and that of nonzeros are x1(x1r~=0)=133604, if that is creating any problem (connecting gap while plotting nonzeros).
2 件のコメント
Are you certain that the elements of x1r are exactly zero or are they only close to zero?
x = [1e6 2.1 3e6 4.2]
x = 1×4
1.0e+06 *
1.0000 0.0000 3.0000 0.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
While the second and fourth elements of x are small enough relative to the first and third elements that they display as 0.0000 they are not in fact equal to 0 as you can see if you extract them.
[x(2), x(4)]
ans = 1×2
2.1000 4.2000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
If you wanted to make anything "small enough" equal to NaN, you can. In this case, I'm defining 10 as "small enough".
x(abs(x) < 10) = NaN
x = 1×4
1000000 NaN 3000000 NaN
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Anil
2025 年 6 月 25 日
Dear @Steven Lord, thank for that. I understand your point. If i plot with a mark '.',, it shows a gap between two points that makes sense that there are no points in between; generally connected by a line.
採用された回答
hello
one method is to use interpolation to fill the voids (NaN)
% Example data with NaN values
x = 1:10;
y = sin([1, 2, NaN, 4, 5, NaN, NaN, 8, 9, 10]/2);
% Interpolate to fill NaN values
y_filled = interp1(x(~isnan(y)), y(~isnan(y)), x, 'linear', 'extrap');
% plot the result
plot(x,y,'*',x,y_filled)

14 件のコメント
Alternative Methods :
1/ Using fillmissing:
% Example data with NaN values
x = 1:10;
y = sin([1, 2, NaN, 4, 5, NaN, NaN, 8, 9, 10]/2);
% Interpolate to fill NaN values
y_filled = fillmissing(y, 'linear'); %This is a simpler and more direct way to fill NaN values using linear interpolation.
% plot the result
plot(x,y,'*',x,y_filled)

2/ Using inpaint_nans : If you need more advanced handling of NaN values, you can use the inpaint_nans function available on MATLAB File Exchange : inpaint_nans - File Exchange - MATLAB Central
Dear @Mathieu NOE, Thanks for that. But I nedd not to fill but skip such points where zeros appear. Marks such as '*','.' and 'other' works fine for a single plot. But if i have 4 or 5 overlapping plots and these marks don't make any snense. Thats why I am curious about line plots with different style (to show overlaps) but avoiding zeros. Thanks.
hello Anil
it would probably help me if you could share your data and the code and also a sketch of what you try to achieve
if your data has NaN (as you did) the line plot will just not show this portion of the plot. Isn't it what you wanted in first place ?
% Example data with NaN values
x = 1:10;
y = sin([1, 2, NaN, 4, 5, NaN, NaN, 8, 9, 10]/2);
% plot the result
plot(x,y)

●
maybe this could be of some interest for you : here I plot several sine waves with some NaN cretad in the y range -0.1 / + 0.1
just in case you are looking for ideas about colrs and line styles (there are tons of available info's and ressources about going beyong standard matlap plotting styles)
your are not obliged to use markers like I did below
t = 0:0.05:2*pi;
MarkerStyle={'o','+','*','x','s','d','v','>'};
% create some new highly distinguishable colors
Colors=[1 0 0;
1 0 1;
0 1 0;
0.4660 0.6740 0.1880;
0 0 1;
0.3010 0.7450 0.9330;
0.8500 0.3250 0.0980;
0.9290 0.6940 0.1250];
figure(1),hold on
for ck = 1:8
y = sin((1+0.2*ck)*t);
y(abs(y)<0.1) = NaN;
p = plot(t,y,'LineWidth',2,'Color',Colors(ck,:),'Marker',MarkerStyle{ck});
leg_str{ck} = (['Line #' num2str(ck)]);
end
legend(leg_str);

●
Anil
2025 年 6 月 25 日
Dear @Mathieu NOE Thanks for that. I am sharing a data file, where I want to plot x(:,1) against x(:,17)........x(:,21) (total 5 line plots). Ooops, zip file size goes above 5MB. How can I share it?
Mathieu NOE
2025 年 6 月 25 日
try with WeTransfer or equivalent (share the link)
Anil
2025 年 6 月 25 日
hello again
try this code below
NB that the last five columns of the x array that you want to plot are numerically equal so the 5 lines will overlay perfectly (if this is waht you wanted ?)
this is a screenshot of my text editor showing the last columns of your file :

this is what I got , once the rows of y data filled with zeroes are removed :

also I used a trick to reduce the amount of data plotted as your file has a ton of rows (maybe the sampling rate is quite high for the job). for a good plot around 1000 points can suffice , no need to have zillions of points.
clc
clearvars
x=readmatrix('test_line.dat');
x1=x(:,1);
y=x(:,17:21);
% now select data for which no row of y is filled with zeros
rowCheck = all(y >eps, 2);
x1 = x1(rowCheck,:);
y = y(rowCheck,:);
% lest downsample a bit the data
% take every r samples to plot
samples = size(x1);
r = 100; % downsampling factor
ind = (1:r:samples);
x1 = x1(ind,:);
y = y(ind,:);
figure
plot(x1,y);
Anil
2025 年 6 月 25 日
Dear @Mathieu NOE, Thanks for that. i have that plot. what i want is line plots but with a gap near -13.5 to -11.5, a flat line connecting two end points should be absent. You can see this (there are no points in between) using any mark '.' insetad of using a line for the plots.
ok, now I see what you want ! :)
see the last figure
this is what I get on my side :

now if you want the same color for the same curve (left and right portions ) this is also doable..
clc
clearvars
x=readmatrix('test_line.dat');
x1=x(:,1);
y=x(:,17:21);
% now select data for which no row of y is filled with zeros
rowCheck = all(y >eps, 2);
x1 = x1(rowCheck,:);
y = y(rowCheck,:);
% lest downsample a bit the data
% take every r samples to plot
samples = size(x1);
r = 100; % downsampling factor
ind = (1:r:samples);
x1 = x1(ind,:);
y = y(ind,:);
figure
plot(x1,y);
% now split the data and plot together the 2 parts
[val,indm] = max(diff(x1)); % find the x discontinuity
figure
plot(x1(1:indm),y(1:indm,:));% left block
hold on
plot(x1(indm+1:end),y(indm+1:end,:));% right block
hold off
a small improvement in the code to show the plots having same color on both sides
to actually show the 5 differents curves (that are normally overlaid) , I added a vertical offset in the form of +0.01*k in those lines :
plot(x1(1:indm),y(1:indm,k)+0.01*k,'Color',colors(k,:));% left block
plot(x1(indm+1:end),y(indm+1:end,:)+0.01*k,'Color',colors(k,:));% right block
plot looks like now :

full code :
clc
clearvars
x=readmatrix('test_line.dat');
x1=x(:,1);
y=x(:,17:21);
% now select data for which no row of y is filled with zeros
rowCheck = all(y >eps, 2);
x1 = x1(rowCheck,:);
y = y(rowCheck,:);
% lest downsample a bit the data
% take every r samples to plot
samples = size(x1);
r = 100; % downsampling factor
ind = (1:r:samples);
x1 = x1(ind,:);
y = y(ind,:);
figure
plot(x1,y);
% now split the data and plot together the 2 parts
[val,indm] = max(diff(x1)); % find the x discontinuity
% define my prefered colors
%% define your custom color order
% option 1 : manually : possible but takes time and not flexible / robust
colors = [0 0 1;...
0 1 0;...
1 0 0;...
0 1 1;...
1 0 1;...
1 0.69 0.39;...
0.6 0.2 0;...
0 0.75 0.75;...
0.22 0.44 0.34;...
0.32 0.19 0.19]; %10x3 RGB array
% % or based on existing color maps
% colors = jet(n);
% or this
% % https://fr.mathworks.com/matlabcentral/fileexchange/42673-beautiful-and-distinguishable-line-colors-colormap
% colors = linspecer(60);
% % even better :
% func = @(x) colorspace('RGB->Lab',x); % https://fr.mathworks.com/matlabcentral/fileexchange/28790-colorspace-transformations
% colors = distinguishable_colors(n,{'w','k'},func); % https://fr.mathworks.com/matlabcentral/fileexchange/29702-generate-maximally-perceptually-distinct-colors
figure
hold on
for k = 1:size(y,2)
plot(x1(1:indm),y(1:indm,k)+0.01*k,'Color',colors(k,:));% left block
plot(x1(indm+1:end),y(indm+1:end,:)+0.01*k,'Color',colors(k,:));% right block
end
hold off
Anil
2025 年 6 月 26 日
linS = {'-','--',':','-.',':'};
l2={'A','B','C','D','E'};
lw={4.3,4.0,3.7,3.4,3.2};
for k = 1:size(y,2)
x0(k)=plot(x1(1:indm),y(1:indm,k),'Color',colors(k,:),'linestyle',linS{k},'LineWidth', lw{k});% left block hold on;
hold on
plot(x1(indm+1:end),y(indm+1:end,:),'Color',colors(k,:),'linestyle',linS{k},'LineWidth', lw{k});% right block
hold on
end
legend(x0([1, 2, 3, 4, 5]),{'$A$','$B$','$c$','$D$','$E$'},'Fontsize',46,'Location','Northwest', 'interpreter', 'latex');
legend('boxoff')
Thank you very much.
ok good
tx for accepting my answer
have a good day
fyi you are not obliged to repeat "hold on" inside the for loop , simply before the for loop is enough
this should give you the same result :
figure
hold on
linS = {'-','--',':','-.',':'};
l2={'A','B','C','D','E'};
lw={4.3,4.0,3.7,3.4,3.2};
for k = 1:size(y,2)
x0(k) = plot(x1(1:indm),y(1:indm,k),'Color',colors(k,:),'linestyle',linS{k},'LineWidth', lw{k});% left block
plot(x1(indm+1:end),y(indm+1:end,:),'Color',colors(k,:),'linestyle',linS{k},'LineWidth', lw{k});% right block
end
legend(x0([1, 2, 3, 4, 5]),{'$A$','$B$','$c$','$D$','$E$'},'Fontsize',46,'Location','Northwest', 'interpreter', 'latex');
legend('boxoff')
hold off
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で 2-D and 3-D Plots についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
