a little bug in function "plot/stem"

when you run code:
a = ones(1179080,1);
stem(a)
as shown in figure,you will find the x axis is constant at the end of data.
why?

7 件のコメント

VBBV
VBBV 2023 年 6 月 9 日
移動済み: VBBV 2023 年 6 月 21 日
a = ones(1179080,1);
stem(a(1:100000:end))
VBBV
VBBV 2023 年 6 月 9 日
移動済み: VBBV 2023 年 6 月 21 日
which version of Matlab are you using ? It seems to work fine
weibo li
weibo li 2023 年 6 月 9 日
移動済み: VBBV 2023 年 6 月 21 日
a = ones(1179080,1);
stem(a)
you should run ”stem(a)“ instead of stem(a(1:100000:end))。Next, zoom into a(1179070:1179080) and insert cursor,you will see that the x-axis of cursor is wrong as following figure.
VBBV
VBBV 2023 年 6 月 9 日
移動済み: VBBV 2023 年 6 月 21 日
The figure is displaying X scale numbers in exponential format. They are NOT constant but varying from 117967 to 119780. You need to change the display format for X axis ticks from exponential to integer or fixed point notation. Then it appearks OK,
VBBV
VBBV 2023 年 6 月 9 日
移動済み: VBBV 2023 年 6 月 21 日
a = ones(1179080,1);
stem(a)
ax= gca;
% zoomed x -scale
xlim([1179070 1179080]);
% use ticks
xticks(1179070:1179080);
% then ticklabels
xticklabels(string(1179070:1:1179080));
grid
Adam Danz
Adam Danz 2023 年 6 月 9 日
移動済み: VBBV 2023 年 6 月 21 日
@VBBV, OP is referring to the x values shown in the data tips.
VBBV
VBBV 2023 年 6 月 9 日
編集済み: VBBV 2023 年 6 月 9 日
It seems the DataTip property uses constant (same) values for every 5 units on X scale. E.g demonstration is shown below. When i use values between 1179070 & 1179075 it shows 1179070 but when i change values between 1179076 & 1179080 it displays 1179080. This is inline with that given in documentation page That means it creates a datatip at the nearest data point and perhaps the default property of datatip for plots in Matlab.
datatip(target,x,y) creates a data tip on the 2-D plotted data point specified by x and y. If you specify approximate coordinates, then datatip creates a data tip at the nearest data point.
v = 1179080;
a = ones(v,1);
s = stem(a);
xlim(v-[10,0])
ylim([0.9,1.1])
%
d = datatip(s,1179071,1);
d.X % Confirms correct placement
ans = 1179071
datatip(s,1179073,1);
datatip(s,1179076,1);
datatip(s,1179080,1);

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

 採用された回答

Adam Danz
Adam Danz 2023 年 6 月 9 日
編集済み: Adam Danz 2023 年 6 月 15 日

1 投票

Reproduce the problem
v = 1179080;
a = ones(v,1);
s = stem(a);
xlim(v-[10,0])
ylim([0.9,1.1])
d = datatip(s,1179071,1);
d.X % Confirms correct placement
ans = 1179071
datatip(s,1179074,1);
datatip(s,1179077,1);
Cause
By default, the datatip format uses a compact floating point format %g. When converted to string,
str2double(sprintf('%g',1179072))
ans
= 1179070
Solution
If you know the data should display as signed integers, specify the format of the DataTipTemplate using dataTipTextRow.
figure()
s = stem(a);
xlim(v-[10,0])
ylim([0.9,1.1])
row = dataTipTextRow('X',1:numel(a),'%d');
s.DataTipTemplate.DataTipRows(1) = row;
datatip(s,1179071,1);
datatip(s,1179074,1);
datatip(s,1179077,1);

1 件のコメント

weibo li
weibo li 2023 年 6 月 21 日
Well done! Thanks!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

タグ

質問済み:

2023 年 6 月 9 日

移動済み:

2023 年 6 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by