Show only two y values in a plot

Hello all,
Any possible way to show only two value (0.02 and 0.04) for the y-axis, because the other values are just noise.
This is what I use for now to plot:
figure()
plot(time,wavelength,'b-x', 'LineWidth',2)
grid
axis tight
xlabel('Time')
ylabel('Wavelength')
hold on
Thank you!

9 件のコメント

Dyuman Joshi
Dyuman Joshi 2022 年 12 月 2 日
Try
idx=ismember(wavelength,[0.02 0.04]);
%ideally one should use ismembertol
plot(time(idx),wavelength(idx),'b-x', 'LineWidth',2)
Hussein Kokash
Hussein Kokash 2022 年 12 月 2 日
Thank you so much! Dyuman Joshi, it works flawlessly!
Hussein Kokash
Hussein Kokash 2022 年 12 月 2 日
Any idea why it is not showing values such as 0.033333333333333 but works well with values such as 0.2 or 0.04, etc?
Thanks!
Dyuman Joshi
Dyuman Joshi 2022 年 12 月 2 日
Because the code I wrote only checks for 0.02 and 0.04, which is what you wanted.
Hussein Kokash
Hussein Kokash 2022 年 12 月 2 日
What I meant that we changing the values to 0.033333333333333 it doesn't show up:
figure(6)
idx=ismember(wavelength,[0.033333333333333, 0.2]);
%ideally one should use ismembertol
plot(time(idx),wavelength(idx),'b-x', 'LineWidth',2)
hold on
This is what is plotted:
Thanks!
Dyuman Joshi
Dyuman Joshi 2022 年 12 月 2 日
This is the problem with floating point numbers, which is why I made the comment.
Try -
idx=ismembertol(wavelength,[0.033333333333333, 0.2],1e-15);
There's still a chance that it might not work, because I don't know the values. If it doesn't, post your data here. Only then can I can say for sure, what you can do.
Hussein Kokash
Hussein Kokash 2022 年 12 月 2 日
Should have posted it earlier, my mistake, here they are:
0.0200000000000000
0.0200000000000000
0.0400000000000000
0.0400000000000000
0.0200000000000000
0.0200000000000000
0.0200000000000000
0.0400000000000000
0.0200000000000000
0.0400000000000000
0.0400000000000000
0.00833333333333333
0.0133333333333333
0.00869565217391304
0.00869565217391304
0.00869565217391304
0.00833333333333333
0.0142857142857143
0.0153846153846154
0.0153846153846154
0.00714285714285714
0.0333333333333333
0.0200000000000000
0.0333333333333333
0.0333333333333333
0.0333333333333333
0.0333333333333333
0.0333333333333333
0.0333333333333333
0.0333333333333333
0.0333333333333333
0.0333333333333333
0.0333333333333333
0.0333333333333333
0.0333333333333333
0.0333333333333333
0.0333333333333333
0.0333333333333333
0.200000000000000
0.200000000000000
0.100000000000000
0.200000000000000
0.200000000000000
0.200000000000000
0.200000000000000
0.200000000000000
0.0500000000000000
0.100000000000000
0.100000000000000
0.200000000000000
0.200000000000000
0.200000000000000
0.200000000000000
0.100000000000000
0.200000000000000
0.200000000000000
0.100000000000000
0.200000000000000
0.200000000000000
0.100000000000000
0.0333333333333333
0.200000000000000
0.200000000000000
0.0666666666666667
0.200000000000000
0.100000000000000
0.200000000000000
0.100000000000000
0.200000000000000
0.200000000000000
0.100000000000000
0.0666666666666667
0.100000000000000
0.0666666666666667
0.0666666666666667
0.200000000000000
0.200000000000000
0.0500000000000000
0.0666666666666667
0.0666666666666667
0.200000000000000
0.200000000000000
0.0400000000000000
0.200000000000000
0.200000000000000
0.200000000000000
0.200000000000000
0.0400000000000000
0.200000000000000
0.200000000000000
0.200000000000000
0.100000000000000
0.200000000000000
0.200000000000000
0.100000000000000
0.0400000000000000
0.200000000000000
0.200000000000000
0.100000000000000
0.200000000000000
0.100000000000000
0.100000000000000
0.200000000000000
0.200000000000000
0.200000000000000
0.200000000000000
0.200000000000000
0.100000000000000
0.200000000000000
0.100000000000000
0.0666666666666667
0.100000000000000
0.200000000000000
0.100000000000000
0.100000000000000
0.200000000000000
0.200000000000000
0.200000000000000
0.100000000000000
0.200000000000000
0.200000000000000
0.200000000000000
0.200000000000000
0.0666666666666667
0.0666666666666667
0.100000000000000
0.100000000000000
Thanks again, appreciate it.
Dyuman Joshi
Dyuman Joshi 2022 年 12 月 2 日
Alright, did my code work? the one with ismembertol() ?
Hussein Kokash
Hussein Kokash 2022 年 12 月 2 日
I changed the 1e-15 to 1e-10 in idx=ismembertol(wavelength,[0.033333333333333, 0.2],1e-15); and now it is working.
Thank you so much! and sorry for the trouble!
Have a great day!

サインインしてコメントする。

回答 (1 件)

KSSV
KSSV 2022 年 12 月 2 日

0 投票

tol = 10^-3 ; % fix your tolerance
idx = abs(wavelength-0.02)<=tol | abs(wavelength-0.04)<=tol ;
plot(time(idx),wavelength(idx),'+r')

カテゴリ

製品

リリース

R2018b

タグ

質問済み:

2022 年 12 月 2 日

コメント済み:

2022 年 12 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by