why while zooming marker changes position?

2019-01-28 10:49发布

问题:

I plot graph and markers on it. However I see strange behavior - marker changes it's position while zooming. I've created video about that (please switch to fullhd while watching to be able to see numbers)

my code:

clear

fDevide = fopen('devideHistory.log');
data = textscan(fDevide, '%f:%f:%f:%f %f,%f %f,%f');
fclose(fDevide);

% hh:min:sec:millisec
secvec = [60*60 60 1 1e-3];
x = [data{1:4}] * secvec';

flvec = [1 1e-16];
y = [data{5:6}] * flvec';

xindays = x / (24*60*60);

plot(xindays, y);
set(gca, 'YTickLabel', get(gca,'YTick'))
datetick('x', 'HH:MM:SS');

hold on

fDeals = fopen('deals.log');
data = textscan(fDeals, '%f:%f:%f:%f %f,%f %f,%f %f,%f %f');
fclose(fDeals);

% hh:min:sec:millisec
secvec = [60*60 60 1 1e-3];
x = [data{1:4}] * secvec';

flvec = [1 1e-16];
y = [data{5:6}] * flvec';

xindays = x / (24*60*60);

plot(xindays, y, 'go','MarkerSize',6,'LineWidth',3);

source files: devideHistory.log deals.log

The question is why marker changes it's position. I expect it to be always at the same position (from file deals.log)

回答1:

It's because you set the y-axis labels

set(gca, 'YTickLabel', get(gca,'YTick'))

when you now start zooming, the ticks themselves change, but the labels don't. You notice this by zooming in and at the same time observing that the range of the y-axis does not change.



I think what you intended to do with the above yticklabel overruling is increase the accuracy of those ytick labels. But you forget to keep those labels up to date with the actual values.

This guy: http://undocumentedmatlab.com/blog/setting-axes-tick-labels-format/ has spent some time of making this an automatic process using callbacks.

On the file exchange the same person has a script to all automate it for you: http://www.mathworks.com/matlabcentral/fileexchange/36254-ticklabelformat

so you only have to call

ticklabelformat(gca,'y','%.6g')

or any other format you want



标签: matlab plot zoom