Potential bug in annotation('arrow') with Parent = gca when zoomed in far
6 views (last 30 days)
Show older comments
There seems to be a bug in annotation('arrow') when its 'Parent' is set to an axis object, i.e. 'gca'. (I got the idea from a how-to on Stackoverflow)
AFAIK one can then use the 'Position' property to specify x0 and the direction or alternatively the start and end point by setting the vectors 'X' and 'Y' - in the graph's coordinate system, which is very convenient.
Matlab then also takes care of moving the annotation around when we use the pan and zoom tools.
The issue arises as soon as I zoom into a plot such that the delta between grid lines is about 0.05. Then the arrow tip (the triangle) and shaft (the line) begin to separate as can be seen in the screenshot (the red arrow has its 'Parent'-object set to the axis and the blue one's parent is the figure object - I use a coordinate transform to calculate the correct on-figure coordinate, but note what happens if this arrow's coordinates get outside the plot's limits... ugly...). Decreasing the arrow's length doesn't help. The separation stays the same.
I run Matlab R2016b/Win. Do you consider that as a bug?
Use the code below to play around with the behavior of the arrow objects. Simply use the pan and zoom tool on the plot.
figure;
h_axis = subplot(1,1,1);
% Arrow whose parent is the subplot (axis object). Can use the coordinates
% directly.
x_ax = [0.3,0.5];
y_ax = [0.5,0.5];
h_arrow_axis = annotation('arrow',x_ax,y_ax,'color',[1,0,0]);
set(h_arrow_axis,'parent',h_axis);
% Arrow whose parent is the figure. Need to transform its coordinates
% first.
x_fig = [0.7,0.5];
y_fig = [0.5,0.5];
[x_t,y_t] = transform_coordinates(h_axis,x_fig,y_fig);
h_arrow_figure = annotation('arrow',x_t,y_t,'color',[0,0,1]);
% Create Callbacks to enable repositioning of the figure-arrow
p = pan;
p.ActionPostCallback = {@pan_zoom_callback_update_arrow,h_axis,h_arrow_figure,x_fig,y_fig};
z = zoom;
z.ActionPostCallback = {@pan_zoom_callback_update_arrow,h_axis,h_arrow_figure,x_fig,y_fig};
function pan_zoom_callback_update_arrow(~,~,axis_handle,arrow_handle,x,y)
%
[x_t,y_t] = transform_coordinates(axis_handle,x,y);
arrow_handle.X = x_t;
arrow_handle.Y = y_t;
end
function [xaf,yaf] = transform_coordinates(h_axis,x,y)
% Excerpt from DS2NFU
% Convert data space units into normalized figure units.
% By Michelle Hirsch
%%Get limits
axun = get(h_axis,'Units');
set(h_axis,'Units','normalized');
axpos = get(h_axis,'Position');
axlim = axis(h_axis);
axwidth = diff(axlim(1:2));
axheight = diff(axlim(3:4));
% Transform data
xaf = (x-axlim(1))*axpos(3)/axwidth + axpos(1);
yaf = (y-axlim(3))*axpos(4)/axheight + axpos(2);
% Restore axes units
set(h_axis,'Units',axun)
end
1 Comment
Accepted Answer
Jan
on 22 Mar 2017
Bug
The problem can be reproduced on my R2016b/Win using this code:
figure;
h_axis = axes;
h_arrow_axis = annotation('arrow', [0.3,0.5], [0.5,0.5], 'color', [1,0,0]);
set(h_arrow_axis, 'parent', h_axis);
Then zoom in.
5 Comments
More Answers (0)
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!