[英]How to find the coordinates of end points of an inserted line in a Matlab figure window
您的情況的主要問題是您的 plot 點和您使用Insert
繪制的線具有相對於不同原點和軸的坐標。
您的散點圖 plot 具有所示軸的坐標(白色矩形),而線具有完整圖形 window (灰色矩形)的坐標。 有關 MATLAB 如何組織此活動的更多詳細信息,請參見此處。
要在軸坐標系中獲取直線端點的坐標,您必須將坐標轉換為該坐標系。
% Random scatter data plotted for example
x = rand(10,1); y = rand(10,1); scatter(x,y)
% Retrieve position values of axes box (in figure coordinates)
ax_pos = get(gca, 'Position');
ax_pos_offset_x = ax_pos(1);
ax_pos_offset_y = ax_pos(2);
ax_pos_width = ax_pos(3);
ax_pos_height = ax_pos(4);
% Retrieve position of line (in figure coordinates, the line needs to be marked in the figure window to allow inspecting it with `gco`
x_line_pos = get(gco, 'X');
y_line_pos = get(gco, 'Y');
% Transform coordinates to axes coordinate system
x_prime = (x_line_pos - ax_pos_offset_x) * diff(xlim) / ax_pos_width;
y_prime = (y_line_pos - ax_pos_offset_y) * diff(ylim) / ax_pos_height;
給
>> x_prime
x_prime =
0.3392 0.6548
>> y_prime
y_prime =
0.6132 0.1865
與 plot 中的行的 position 匹配
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.